Created
December 17, 2018 18:00
-
-
Save davidhemphill/2f90d209f047f9d46b3c7dc39790982a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, this will be helpful to not have to make many versions of a class just to replace "<", "<=", etc...