Skip to content

Instantly share code, notes, and snippets.

@davidhemphill
Created December 17, 2018 18:00
Show Gist options
  • Save davidhemphill/2f90d209f047f9d46b3c7dc39790982a to your computer and use it in GitHub Desktop.
Save davidhemphill/2f90d209f047f9d46b3c7dc39790982a to your computer and use it in GitHub Desktop.
<?php
namespace App\Nova\Filters;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Container\Container;
use Laravel\Nova\Filters\DateFilter as NovaDateFilter;
class DateFilter extends NovaDateFilter
{
public $column;
public function __construct($name, $column)
{
$this->name = $name;
$this->column = $column;
}
/**
* Get the key for the filter.
*
* @return string
*/
public function key()
{
return "date_filter_{$this->column}";
}
/**
* 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)
{
if ($this->column == 'created_at') {
$query->where($this->column, '<', Carbon::parse($value));
}
if ($this->column == 'updated_at') {
$query->where($this->column, '>', Carbon::parse($value));
}
return $query;
}
}
@tristanbailey
Copy link

Thanks, this will be helpful to not have to make many versions of a class just to replace "<", "<=", etc...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment