Skip to content

Instantly share code, notes, and snippets.

@tristanbailey
Forked from davidhemphill/DateFilter.php
Created December 18, 2018 11:34
Show Gist options
  • Save tristanbailey/614109999ab24064c5cde3e9472d86d8 to your computer and use it in GitHub Desktop.
Save tristanbailey/614109999ab24064c5cde3e9472d86d8 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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment