Brand new to backpack still, and trying to understand the grouping option.
I have a polymorphic Flag
table with flaggable_id
and flaggable_type
I want to group my query by both of these fields and then order them by the highest count.
protected function setupListOperation()
{
// Group the flags by flaggable_id and flaggable_type and select the necessary columns
CRUD::addClause('select', 'flaggable_id', 'flaggable_type', DB::raw('count(*) as flag_count'));
CRUD::groupBy(['flaggable_id', 'flaggable_type']);
CRUD::orderBy('flag_count', 'desc');
$this->crud->addColumns([
[
'name' => 'flaggable_type',
'label' => 'Model Type',
'type' => 'custom_html',
'value' => function($entry) {
return class_basename($entry->flaggable_type);
},
],
[
'name' => 'flag_count',
'label' => 'Flag Count',
'type' => 'number',
],
[
'name' => 'post_message',
'label' => 'Post Message',
'type' => 'custom_html',
'value' => function($entry) {
if ($entry->flaggable_type === 'App\Models\Post') {
$message = AppModelsPost::find($entry->id)->message;
return $message;
}
return ''; // Return an empty string if the flaggable_type is not a post
},
],
]);
}
The query this produces still tries to order by ‘id’ but I’m not selecting ‘id’.
select `flaggable_id`, `flaggable_type`, count(*) as flag_count from `flags` group by `flaggable_id`, `flaggable_type` order by `flag_count` desc, `id` desc limit 10
I’m not sure what I’m doing wrong here. Any help appreciated.