7-06 1,267 views
laravel-admin默认的between->datetime(),查询默认是datetime类型,但是假如数据库是时间戳类型就会报错,又不想改底层文件的话可以试试加自定义筛选功能。
1.首先定义新的between class,主要加了一个 $timestamp,和condition方法中数据库查询的判断
<?php
namespace App\Lib\Grid;
use Encore\Admin\Admin;
use Encore\Admin\Grid\Filter\AbstractFilter;
use Illuminate\Support\Arr;
class MyBetween extends AbstractFilter
{
/**
* {@inheritdoc}
*/
protected $view = 'admin::filter.between';
protected $timestamp = false;
/**
* Format id.
*
* @param string $column
*
* @return array|string
*/
public function formatId($column)
{
$id = str_replace('.', '_', $column);
return ['start' => "{$id}_start", 'end' => "{$id}_end"];
}
/**
* Format two field names of this filter.
*
* @param string $column
*
* @return array
*/
protected function formatName($column)
{
$columns = explode('.', $column);
if (count($columns) == 1) {
$name = $columns[0];
} else {
$name = array_shift($columns);
foreach ($columns as $column) {
$name .= "[$column]";
}
}
return ['start' => "{$name}[start]", 'end' => "{$name}[end]"];
}
/**
* Get condition of this filter.
*
* @param array $inputs
*
* @return mixed
*/
public function condition($inputs)
{
if ($this->ignore) {
return;
}
if (!Arr::has($inputs, $this->column)) {
return;
}
$this->value = Arr::get($inputs, $this->column);
$value = array_filter($this->value, function ($val) {
return $val !== '';
});
if (empty($value)) {
return;
}
if (!isset($value['start'])) {
return $this->buildCondition($this->column, '<=', $value['end']);
}
if (!isset($value['end'])) {
return $this->buildCondition($this->column, '>=', $value['start']);
}
$this->query = 'whereBetween';
//下面这4行
if ($this->timestamp) {
$this->value['start'] = strtotime($this->value['start']);
$this->value['end'] = strtotime($this->value['end']);
}
return $this->buildCondition($this->column, $this->value);
}
/**
* @param array $options
*
* @return $this
*/
public function datetime($options = [], $timestamp = false)
{
$this->view = 'admin::filter.betweenDatetime';
$this->setupDatetime($options);
$this->timestamp = $timestamp;
return $this;
}
/**
* @param array $options
*/
protected function setupDatetime($options = [])
{
$options['format'] = Arr::get($options, 'format', 'YYYY-MM-DD HH:mm:ss');
$options['locale'] = Arr::get($options, 'locale', config('app.locale'));
$startOptions = json_encode($options);
$endOptions = json_encode($options + ['useCurrent' => false]);
$script = <<<EOT
$('#{$this->id['start']}').datetimepicker($startOptions);
$('#{$this->id['end']}').datetimepicker($endOptions);
$("#{$this->id['start']}").on("dp.change", function (e) {
$('#{$this->id['end']}').data("DateTimePicker").minDate(e.date);
});
$("#{$this->id['end']}").on("dp.change", function (e) {
$('#{$this->id['start']}').data("DateTimePicker").maxDate(e.date);
});
EOT;
Admin::script($script);
}
}
2.Admin\bootstrap.php中新增一行
\Encore\Admin\Grid\Filter::extend('myBetween',MyBetween::class);
3.在column中就可以用自定义的between了
$filter->column(1 / 2, function (Grid\Filter $filter) {
$filter->myBetween('xxx', '日期1')->datetime([],true);
$filter->myBetween('xxx', '日期2')->datetime([],true);
});
参考文章:https://blog.csdn.net/qq_37500838/article/details/103302225