先使用规则选择器,自定义查询创建数仓,注册服务,绑定在接口中创建方法在实现中编写逻辑进行传参判断
1 新建仓库目录
app
/Repository
|--app
/Repository
/Interfaces
|--app
/Repository
/Repositories
2.定义接口
app
/Repository
/Interfaces
/GoodsZpInterface
.php
namespace App\Repository\Interfaces;
Interface GoodsZpInterface
{
public function getTimeStart($value);
}
3 接口实现
namespace App\Repository\Repositories;
use App\Admin\Models\GoodsZp;
use App\Repository\Interfaces\GoodsZpInterface;
class GoodsZpRepository Implements GoodsZpInterface
{
public function getTimeStart($value)
{
$time = date("Y-m-d H:i:s");
if ($value == 1) {
$data = GoodsZp
::where('starts_at', '>', $time)->where('sort', '1')->pluck('id')->toArray();
return $data;
}
if ($value == 2) {
$data = GoodsZp
::where('starts_at', '<=', $time)->where('sort', '1')->where('ends_at', '>=', $time)->pluck('id')->toArray();
return $data;
}
if ($value == 3) {
$data = GoodsZp
::where('ends_at', '<', $time)->where('sort', '1')->pluck('id')->toArray();
return $data;
}
if ($value == '') {
$data = GoodsZp
::pluck('id');
return $data;
}
if ($value == 4) {
$data = GoodsZp
::where('sort', '0')->pluck('id');
return $data;
}
}
}
4 接口绑定实现 4.1 创建一个服务
php artisan make
:provider RepositoryServiceProvider
1 命令执行后会在 app/Providers 下创建 RepositoryServiceProvider.php 服务
4.2 将服务配置到应用中app/config/app.php 中配置
'providers' => [
...
App\
Providers\RepositoryServiceProvider::class,
]
4.3 绑定接口 app/Providers/RepositoryServiceProvider.php
public function register()
{
$this->app->bind('App\Repository\Interfaces\TestInterface', 'App\Repository\Repositories\TestRepository');
}
5 仓库调用
在larverl admin 中调用实现
protected $goods;
public function __construct(GoodsZpInterface
$goodsZpInterface)
{
$this->goods = $goodsZpInterface;
}
$grid->selector(function (Grid\
Tools\Selector $selector) {
$selector->select('id', '活动状态', [
'' => '所有活动',
1 => '未开始',
2 => '进行中',
3 => '已结束',
4 => '已失效',
], function ($query, $value) {
$arr = $this->goods->getTimeStart($value[0]);
if (count($arr) > 1) {
$query->whereBetween('id', $arr);
} else {
$query->where('id', $arr[0]);
}
});
});
<?php
namespace App\Admin\Controllers;
use App\Admin\Actions\Agent\Detail;
use App\Admin\Actions\Post\Restore;
use App\Admin\Models\GoodsZp;
use App\Repository\Interfaces\GoodsZpInterface;
use Encore\Admin\Admin;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Tab;
use Encore\Admin\Widgets\Table;
class GoodsZpController extends AdminController
{
protected $goods;
public function __construct(GoodsZpInterface
$goodsZpInterface)
{
$this->goods = $goodsZpInterface;
}
protected $title = 'GoodsZp';
protected function grid()
{
$grid = new Grid(new GoodsZp());
$grid->selector(function (Grid\
Tools\Selector $selector) {
$selector->select('id', '活动状态', [
'' => '所有活动',
1 => '未开始',
2 => '进行中',
3 => '已结束',
4 => '已失效',
], function ($query, $value) {
$arr = $this->goods->getTimeStart($value[0]);
if (count($arr) > 1) {
$query->whereBetween('id', $arr);
} else {
$query->where('id', $arr[0]);
}
});
});
$grid->filter(function ($filter) {
$filter->scope('trashed', '回收站')->onlyTrashed();
});
$grid->actions(function ($actions) {
if (\
request('_scope_') == 'trashed') {
$actions->add(new Restore());
}
});
$grid->column('id', __('Id'));
$grid->column('title', __('Title'));
$grid->column('goods_no', __('Goods no'));
$grid->column('number', __('Number'));
$grid->column('price', __('Price'));
$grid->column('rate', __('Rate'));
$grid->column('sort', __('Sort'))->using([
1 => '正常',
0 => '失效'
]);
$grid->column('get_point', __('Get point'));
$grid->column('starts_at', __('Starts at'));
$grid->column('ends_at', __('Ends at'));
$grid->column('created_at', __('Created at'));
$grid->column('updated_at', __('Updated at'));
$grid->column('deleted_at', __('Deleted at'));
return $grid;
}