larverl admin规格选择器 传参查询 使用数仓实现

tech2024-07-14  55

先使用规则选择器,自定义查询创建数仓,注册服务,绑定在接口中创建方法在实现中编写逻辑进行传参判断

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 接口实现

//app/Repository/Repositories/GoodsZpRepository.php namespace App\Repository\Repositories; use App\Admin\Models\GoodsZp; use App\Repository\Interfaces\GoodsZpInterface; class GoodsZpRepository Implements GoodsZpInterface { public function getTimeStart($value) { // TODO: Implement getTimeStart() method. $time = date("Y-m-d H:i:s"); if ($value == 1) { //传值为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

/** * Register the application services. * * @return void */ 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) { //$value[0] 为select中的被点击一条数据的key //调用接口中的方法getTimeStart 将被点击的key作为参数传入 $arr = $this->goods->getTimeStart($value[0]); //在getTimeStart方法中判断 if (count($arr) > 1) { //获取到的数据大于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; } /** * Title for current resource. * * @var string */ protected $title = 'GoodsZp'; /** * Make a grid builder. * * @return Grid */ 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) { // 范围过滤器,调用模型的`onlyTrashed`方法,查询出被软删除的数据。 $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; }
最新回复(0)