I started a project almost a month ago and created a simple table structure for handling categories with "self-referencing association" if you can call it that.
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->foreignId('parentId')->nullable()->constrained('categories')->cascadeOnDelete();
$table->unsignedBigInteger('guildId')->nullable();
$table->string('title');
$table->bigInteger('count')->default(0);
$table->string('type')->default('product')->index();
$table->string('slug')->unique();
$table->text('description')->nullable();
$table->text('full_description')->nullable();
$table->unsignedBigInteger('sort_order')->nullable();
$table->foreignId('thumbnailId')->nullable()->constrained('files')->nullOnDelete();
$table->enum('status', ['disabled', 'enabled'])->default('enabled');
$table->timestamps();
});
}
The model itself is also really simple.
public function parent()
{
return $this->belongsTo(Category::class, 'parentId');
}
public function parents()
{
return $this->belongsTo(Category::class, 'parentId')->with('parents');
}
public function children()
{
return $this->hasMany(Category::class, 'parentId');
}
public function products()
{
return $this->hasMany(Product::class, 'rootCategoryId');
}
public function allProducts()
{
return $this->belongsToMany(Product::class, 'category_product', 'categoryId', 'productId');
}
This works just fine, but I need now some functionality that is hard to make it work with this kind of table structure. Btw, only leaf-categories or final categories contain products/items. Three is no requirement to fetch products by landing in the middle of the tree. Only the total count of products from that category node and down.
What can't be done (or I didn't figure out yet how to)
- Counting total products from the root category down to (or up from) the leaf-categories.
- Landing in the middle of the category tree and counting products down (or up from) the leaf categories children.
- While products are inactive (I have global and local scopes for this), some category children will be empty. Those must not be displayed or fetched from the database.
Also, keep in mind that I currently have 7 root or top categories from which the tree unfolds or think of it as 7 branches of categories and sub-categories. Those 7 roots do not have a parent_id, it is either null or 0, meaning they don't belong to a "master" parent.
Product.php
class Product extends Model
{
public function category()
{
return $this->belongsTo(Category::class); // products table has the category_id column
}
}
Some expected results would be like the following. Landing on the "index" should render the 7 categories (or any number of root categories) with the total product count.
- Category 1 (600 products)
- Category 1 (500 products)
- ...
- Category nth (x products)
If you land in the middle of the category tree it should function similarly.
From Category 1 let's say it's immediate children which is also not a leaf category, but a parent to others.
etc, I hope this makes sense.
It is also not required to be "unfolded" like that, I've only placed children of children for this example just for simplicity and to understand the structure. Whenever you land in the middle of the tree, it requires to render only the immediate children of that category node, with the count of total products (including the landed parent)
For example:
If landed on Category 1, only render the immediate children
Title of parent: Parent Category Name (X products)
- Child 1 (x products)
- Child 2 (x products)
- ...
- Child nth (x products)
My category structure can go very deep, it's very detailed and it is a requirement to be very very specific like that. I have a total of 3200 categories containing 2.4m products. if you wonder about performance issues. Some categories are completely empty. but are there waiting to be filled.
Is it even possible with this single-table structure to do what I require? I feel burned and can't think of a way anymore how to solve this.
Sorry for the long post, I hope someone could help me figure this out.
Controller.php
public function index(Request $request)
{
$query = Category::ofType($this->type)->with('parent')->with('products')->orderBy('title');
if ($request->filled('search') ) {
if (is_numeric($request['search']) ) {
$query->where('id', 'LIKE', "%${request['search']}%");
} else {
$query->where('title', 'LIKE', "%${request['search']}%");
$query->orWhereHas('parent', function ($query) use ($request) {
return $query->where('title', 'LIKE', "%${request['search']}%");
});
}
}
return view('category::admin.index', [
'categories' => $query->paginate(20)->appends($request->except('page')),
]);
}
index.blade.php
@php
function plusCountCategory( &$category ) {
foreach($categories as $category){
if( !! $category["parentId"] )
{
$category["count"] = $category["count"] + 1 ;
plusCountCategory($category["parentId"] ) ;
}
}
}
@endphp
It has an error