How to add date filter with relationship_count? #1292
Replies: 3 comments
-
To add a date filter with relationship_count, you need to modify your approach slightly. The
$this->crud->query->withCount(['notes' => function ($query) {
// This will be modified by the filter
}]);
CRUD::filter('datetime')
->label('Date')
->type('date_range')
->whenActive(function ($value) {
$dates = json_decode($value);
$this->crud->query->withCount(['notes' => function ($query) use ($dates) {
$query->where('updated_at', '>=', $dates->from)
->where('updated_at', '<=', $dates->to . ' 23:59:59');
}]);
});
[
'name' => 'notes_count',
'type' => 'text',
'label' => 'Total notes',
'suffix' => ' notes',
], This approach will be more performant and allow you to filter the notes count by date. The Here are some important points to note:
Remember to test this thoroughly with your specific data structure and requirements. Backpack Docs: Columns ℹ️ Please note our AI Bot does not reply to follow-ups, this will be the only AI answer in this thread. If the AI helped you find the answer, please mark it as answered or write details below so it can learn. Then close the conversation. If not, please give more information for when a human reads this. ℹ️ Relevant sources: Github Issue (closed): Filter for CRUD (DateRange) not working |
Beta Was this translation helpful? Give feedback.
-
Hello @blessen131 First, as our bot show, your date_range is nos correctly set, you set
You can follow bot instructions or try this:
Let me know if that's work for you. Cheers. |
Beta Was this translation helpful? Give feedback.
-
@jcastroa87 Still not working!
User Model
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to find how many notes each user added and filter by date, everything is working except the date filter
//TODO:: date filter
User Model
Beta Was this translation helpful? Give feedback.
All reactions