VarboxInternational / varbox

THE Laravel Admin Panel package built by developers, for developers
https://varbox.io
Other
63 stars 13 forks source link

Filter records by date #16

Closed andreiamza closed 3 years ago

andreiamza commented 3 years ago

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:

How can I filter my customers by last_login_date using these two fields as date limits?

zbiller commented 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:

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:

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();