group table rows by morph column

Hi, i have a price lines table with a parent morph table. A price line can be added to a Order or TransportOrder. I would like to group the table by the unique combination of parent_type and parent_id but i can't get it to work... I have tried modifying query and then calling the default group on the column with parent_unique:
->modifyQueryUsing(function (Builder $query) {
return $query->selectRaw('*, CONCAT(parent_type, " ", parent_id) as parent_unique');
})
->modifyQueryUsing(function (Builder $query) {
return $query->selectRaw('*, CONCAT(parent_type, " ", parent_id) as parent_unique');
})
I tried adding an attribute to the PriceLine model but i think the group by is executed as an sql query so the attribute does not work. It does load in the table of course but i cant group on it... Also tried making a group column via the Group class:
Group::make('parent')
->label('Parent')
->groupQueryUsing(fn($query): string => $query->groupBy(['parent_id','parent_type'])),
Group::make('parent')
->label('Parent')
->groupQueryUsing(fn($query): string => $query->groupBy(['parent_id','parent_type'])),
I really want this grouping because this table will generate invoice lines and i want to use the bulk select to do this. Any help is appreciated!
Solution:
You need both the titleFromRecord AND the groupQueryUsing. This does work. made an attribute on price line and now it groups correct. ```php Group::make('parent_id') ->label('Parent')...
No description
Jump to solution
1 Reply
Solution
gladjanus43
gladjanus432mo ago
You need both the titleFromRecord AND the groupQueryUsing. This does work. made an attribute on price line and now it groups correct.
Group::make('parent_id')
->label('Parent')
->getTitleFromRecordUsing(
fn (PriceLine $record): string => $record->parentName
)
->groupQueryUsing(fn ($query) => $query->groupBy('parentName')),
Group::make('parent_id')
->label('Parent')
->getTitleFromRecordUsing(
fn (PriceLine $record): string => $record->parentName
)
->groupQueryUsing(fn ($query) => $query->groupBy('parentName')),
No description

Did you find this page helpful?