Skip to content
This repository was archived by the owner on Nov 30, 2018. It is now read-only.

Date range filter #9

Closed
outhebox opened this issue Nov 9, 2018 · 6 comments
Closed

Date range filter #9

outhebox opened this issue Nov 9, 2018 · 6 comments

Comments

@outhebox
Copy link

outhebox commented Nov 9, 2018

Any documentation to how make a range filter [from - to] like the main demo?

@beliolfa
Copy link
Contributor

beliolfa commented Nov 9, 2018

You have to create two different filters. Then import both into your Resource like this:

public function filters(Request $request)
    {
        return [
            new DateFrom,
            new DateTo
        ];
    }

@beliolfa beliolfa closed this as completed Nov 9, 2018
@outhebox
Copy link
Author

outhebox commented Nov 9, 2018

@disitec Thanks for the fast reply, but i need to know how can i use where between with apply method in the two different filters as you mentioned, idk how exactly preform the query this?

@beliolfa
Copy link
Contributor

beliolfa commented Nov 9, 2018

Treat each filter as independent. Then in the apply method of each one, add a new constraint

// DateFrom
public function apply(Request $request, $query, $value)
{
    return $query->where('date', '>=', $value);
}

// DateTo
public function apply(Request $request, $query, $value)
{
    return $query->where('date', '<=', $value);
}

@outhebox
Copy link
Author

outhebox commented Nov 9, 2018

Thank you (Y)

@nasrulhazim
Copy link

Here how I did, based on comments above:

The Abstract Class

<?php

namespace App\Nova\Filters;

use Illuminate\Http\Request;
use R64\Filters\DateFilter;

abstract class Date extends DateFilter
{
    /**
     * Is the Date Filter is use for Date From.
     *
     * @var bool
     */
    public $is_date_from = false;

    /**
     * Is the Date Filter is use for Date To.
     *
     * @var bool
     */
    public $is_date_to = false;

    /**
     * Determine if the Date filter use for Date From.
     * 
     * @return boolean 
     */
    public function isDateFrom()
    {
        return $this->is_date_from;
    }

     /**
     * Determine if the Date filter use for Date To.
     * 
     * @return boolean 
     */
    public function isDateTo()
    {
        return $this->is_date_to;
    }

    /**
     * Get Date Operand.
     *
     * @return string
     */
    public function getOperand()
    {
        if ($this->isDateFrom()) {
            return '>=';
        }

        if ($this->isDateTo()) {
            return '<=';
        }

        return '=';
    }

    /**
     * Get Date Field From $date_field property.
     *
     * @return string
     */
    public function getDateField()
    {
        return $this->date_field;
    }

    /**
     * Apply the filter to the given query.
     *
     * @param \Illuminate\Http\Request              $request
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @param mixed                                 $value
     *
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function apply(Request $request, $query, $value)
    {
        return $query->where(
            $this->getDateField(),
            $this->getOperand(),
            $value
        );
    }

    /**
     * Get the filter's available options.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return array
     */
    public function options(Request $request)
    {
        return [
            'dateFormat' => 'Y-m-d',
        ];
    }
}

Filter Setup

namespace App\Nova\Filters;

class DateFrom extends Date
{
    public $date_field = 'created_at';

    public $is_date_from = true;
}
namespace App\Nova\Filters;

class DateTo extends Date
{
    public $date_field = 'created_at';

    public $is_date_to = true;
}

The Usage

...
    public function filters(Request $request)
    {
        return [
            new Filters\DateFrom(),
            new Filters\DateTo(),
        ];
    }
...

@nasrulhazim
Copy link

I have submitted PR as well - #10

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants