Skip to content
Advertisement

Laravel sync users to all of categoires which they can be unlimited

In our web application each user can create multiple nested category by selecting the parent which that specified with category_id column.

Schema::create('categories', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->unsignedBigInteger('category_id')->index()->nullable();
    $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
    $table->string('category_name');

    ...

    $table->timestamp('created_at')->useCurrent();
    $table->timestamp('updated_at')->useCurrent();
});

now you supposed one of our user created this category structure:

enter image description here

multiple nested categories children are unlimited, that means every category can be have child and we can’t use any static or fixed number for example in for statement to sync the category to specify users.

my question is how can i set specify users to all of categories from parent?

this query only can set users for single child of parent. you can suppose 1,3,4,5,6,7 in above screen shot and not working for 2 and all of their children

children of parent category can be unlimited and how can we set users from the parent to all of nested and children categories?

$category = Category::whereId($request->category_id)->whereNull('category_id')->with('childrenCategories')->first();

$category->users()->sync($request->users);
foreach ($category->childrenCategories as $category) {
    $category->users()->sync($request->users);
}

simplified output of query:

AppEntitiesCategory {#2114 ▼
  ...
  #attributes: array:11 [▶]
  #original: array:11 [▶]
  ...
  #relations: array:1 [▼
    "childrenCategories" => IlluminateDatabaseEloquentCollection {#2118 ▼
      #items: array:4 [▼
        0 => AppEntitiesCategory {#2133 ▶}
        1 => AppEntitiesCategory {#2134 ▶}
        2 => AppEntitiesCategory {#2135 ▼
          ...
          #attributes: array:11 [▶]
          #original: array:11 [▶]
          ...
          #relations: array:1 [▼
            "categories" => IlluminateDatabaseEloquentCollection {#2138 ▼
              #items: array:1 [▼
                0 => AppEntitiesCategory {#2157 ▼
                  ...
                  #attributes: array:11 [▶]
                  #original: array:11 [▶]
                  ...
                  #relations: []
                  ...
                }
              ]
            }
          ]
          ...
        }
        3 => AppEntitiesCategory {#2136 ▶}
      ]
    }
  ]
  ...
}

Advertisement

Answer

You have to use a recursive function like below:

    private function syncCategories($category,$request){

        if($category->childrenCategories->first()){
             foreach ($category->childrenCategories as $childCategory) {
                 $childCategory->users()->sync($request->users);
                 $this->syncCategories($childCategory);
              }
         }
     }
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement