Closed andreiamza closed 3 years ago
Given what you've said, most of the work is already done.
You just need to instruct your CustomerFilter
class to filter by start
and end
accordingly, treating those fields as dates.
The Varbox\Filters\Filter
class already offers a few date operators by default:
OPERATOR_DATE
OPERATOR_DATE_EQUAL
OPERATOR_DATE_NOT_EQUAL
OPERATOR_DATE_SMALLER
OPERATOR_DATE_GREATER
OPERATOR_DATE_SMALLER_OR_EQUAL
OPERATOR_DATE_GREATER_OR_EQUAL
Here is the documentation section for reference: https://varbox.io/docs/2.x/filter-records#available-filter-operators
In your case, you should configure your CustomerFilter
class to work with one of the following combinations:
'start' => Filter::OPERATOR_DATE_GREATER
and 'end' => Filter::OPERATOR_DATE_SMALLER
'start' => Filter::OPERATOR_DATE_GREATER_OR_EQUAL
and 'end' => Filter::OPERATOR_DATE_SMALLER_OR_EQUAL
Personally, I will choose the latter, resulting in the following:
<?php
namespace App\Filters;
use Varbox\Filters\Filter;
class YourFilter extends Filter
{
/**
* Get the filters that apply to the request.
*
* @return array
*/
public function filters()
{
return [
'start' => [
'operator' => Filter::OPERATOR_DATE_GREATER_OR_EQUAL,
'condition' => Filter::CONDITION_OR,
'columns' => 'start',
],
'end' => [
'operator' => Filter::OPERATOR_DATE_SMALLER_OR_EQUAL,
'condition' => Filter::CONDITION_OR,
'columns' => 'status',
],
];
}
/**
* Get the main where condition between entire request fields.
*
* @return string
*/
public function morph()
{
return 'and';
}
/**
* Get the modified value of a request filter field.
*
* @return array
*/
public function modifiers()
{
return [];
}
}
That's it. Now you can filter your customers from your controller:
Customer::filtered($request->all(), new CustomerFilter)->get();
What are you trying to achieve (screenshots might help):
I'm trying to filter my customers by last login date using the Varbox's built in filtering functionality.
What I have so far:
start
andend
), which act as delimiters in which I can specify dates in theYYYY-mm-dd
formatCustomerFilter
class that I created based on the documentationHow can I filter my customers by
last_login_date
using these two fields as date limits?