F
Filament2mo ago
loco

Grouping Rows with Closure/Tree relation

So i have a table in this shape: Schema::create('products', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->unsignedInteger('parent_product_id')->nullable(); //foreign key to self }); Schema::create('products_closure', function (Blueprint $table) { $table->unsignedInteger('ancestor'); $table->unsignedInteger('descendant'); $table->unsignedTinyInteger('distance'); $table->primary(['ancestor', 'descendant']); }); I want to group them by the root(topmost ancestor) I tried to do this Group::make('name') ->getTitleFromRecordUsing(fn (Product $record) => $record->getRoot()->name ?? $record->name) ->getKeyFromRecordUsing(fn (Product $record) => $record->getRoot()->id ?? $record->id) but it doesnt seem to group. It shows as such Any suggestions? The first and third group should be one but because the name differs, they are seperated
No description
3 Replies
nanopanda
nanopanda2mo ago
I'm not sure how getKeyFromRecordUsing functions internally, but based on your output it looks like it's still doing the actual group partitioning based on the "name" attribute of the product. I think you would need to make the attribute passed in to Group::make() an actual relation, like "parent.name" for the DB query to be output correctly.
Zen Nitiruj
Zen Nitiruj2mo ago
Because you say group('name'), then it group by name. If you wan to group by parent_product_id, just say group('parent_product_id')
loco
locoOP2mo ago
But it wouldnt group by the root but instead by the immediate parent Yeah, thats the issue. there is no actual attribute to pass to Group::make() Need a way to override Group::make() so i dont have to use an actual relation but use the name because it seems to work 90%
Want results from more Discord servers?
Add your server