商品表:
CREATE TABLE `goods` ( `id` int(10) NOT NULL AUTO_INCREMENT, `goods_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `goods_price` decimal(10, 2) NULL DEFAULT NULL, `goods_stock` int(5) NULL DEFAULT NULL, `goods_no` int(10) NULL DEFAULT NULL, `is_pt` int(1) NULL DEFAULT NULL, `deleted_at` timestamp(0) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;团购表:
CREATE TABLE `pt_goods` ( `id` int(10) NOT NULL AUTO_INCREMENT, `pt_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `goods_id` int(10) NULL DEFAULT NULL, `pt_price` decimal(10, 2) NULL DEFAULT NULL, `start_time` datetime(0) NULL DEFAULT NULL, `end_time` datetime(0) NULL DEFAULT NULL, `status` int(1) NULL DEFAULT NULL, `order` int(6) NULL DEFAULT NULL, `pt_num` int(1) NULL DEFAULT NULL, `deleted_at` timestamp(0) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;运行下面命令创建模型
php artisan make:model Pt模型代码
class Pt extends Model { //软删除 use SoftDeletes; protected $table='自己创建的表'; //关联商品表 public function goods(){ return $this->belongsTo(Goods::class,'goods_id','id'); } //根据活动时间 修改活动状态 public function getStatusAttribute($status) { if ($status == 3) { return 3; }elseif ($this->start_time > date('Y-m-d H:i:s')){ if ($status!=0){ $this->status=0; $this->push(); } return 0; }elseif ($this->end_time >date('Y-m-d H:i:s')){ if ($status!=1){ $this->status=1; $this->push(); } return 1; }elseif ($this->end_time<date('Y-m-d H:i:s')){ if ($status!=2){ $this->status=2; $this->push(); } return 2; } } }示例:下面代码省略了无需修改部分
运行下面命令创建控制器
php artisan admin:make PtController --model=App\Pt <?php class PtController extends AdminController { protected $PostRepository; protected $Pt; public function __construct( PostRepository $PostRepository, Pt $Pt) { $this->PostRepository=$PostRepository; $this->Pt=$Pt; } protected function grid() { $tab=new Tab(); $all=\Admin::grid($this->Pt,function (Grid $grid){ $grid->column('id', __('Id')); $grid->model()->orderBy('order'); $grid->actions(function ($actions){ if ($actions->row['status']==0 || $actions->row['status']==1){ $actions->disableDelete(); $actions->add(new Replicate()); } }); $grid->column('pt_name', __('活动名称')); $grid->column('goods.goods_name', __('商品名称')); $grid->column('status', __('状态'))->using([ 0=>'未开始', 1=>'进行中', 2=>'已结束', 3=>'已失效', ]); }); $nostart=\Admin::grid($this->Pt,function (Grid $grid){ $grid->model()->where('status',0); $grid->model()->orderBy('order'); $grid->actions(function ($actions){ $actions->disableDelete(); $actions->add(new Replicate()); }); $grid->column('id', __('Id')); $grid->column('pt_name', __('活动名称')); $grid->column('goods.goods_name', __('商品名称')); $grid->column('status', __('状态'))->display(function (){ return '未开始'; }); }); $starting=\Admin::grid($this->Pt,function (Grid $grid) { $grid->model()->where('status', 1); $grid->model()->orderBy('order'); $grid->actions(function ($actions) { $actions->disableDelete(); $actions->add(new Replicate()); }); $grid->column('id', __('Id')); $grid->column('pt_name', __('活动名称')); $grid->column('goods.goods_name', __('商品名称')); $grid->column('status', __('状态'))->display(function () { return '进行中'; }); }); $end=\Admin::grid($this->Pt,function (Grid $grid){ $grid->model()->where('status',2); $grid->model()->orderBy('order'); $grid->column('id', __('Id')); $grid->column('pt_name', __('活动名称')); $grid->column('goods.goods_name', __('商品名称')); $grid->column('status', __('状态'))->display(function (){ return '已结束'; }); }); $loser=\Admin::grid($this->Pt,function (Grid $grid){ $grid->model()->where('status',3); $grid->model()->orderBy('order'); $grid->column('id', __('Id')); $grid->column('pt_name', __('活动名称')); $grid->column('goods.goods_name', __('商品名称')); $grid->column('status', __('状态'))->display(function (){ return '已失效'; }); }); $tab->add('全部活动',$all); $tab->add('未开始',$nostart); $tab->add('进行中',$starting); $tab->add('已结束',$end); $tab->add('已失效',$loser); return $tab; } protected function detail($id) { $show = new Show(Pt::findOrFail($id)); $show->field('status', __('状态'))->using([ 0=>'未开始', 1=>'进行中', 2=>'已结束', 3=>'已失效', ]); return $show; } protected function form() { $form = new Form(new Pt()); $form->text('pt_name', __('活动名称')); $form->number('goods_id', __('选择商品'))->options($this->PostRepository->getGoods()); $form->number('status', __('排序'))->default(9)->help('越小越靠前'); return $form; } }在app\Admin\routes.php配置路由
$router->resource('pts', PtController::class);终端运行下面命令添加自定义行操作
php artisan admin:action Post\Replicate --grid-row --name="使失效"在app\Admin\Action\ Post\Replicate.php下编辑行操作
namespace App\Admin\Actions\Post; use Encore\Admin\Actions\RowAction; use Illuminate\Database\Eloquent\Model; class Replicate extends RowAction { public $name = '使失效'; public function handle(Model $model) { $query=$model->find($this->getKey()); $query->status=3; $query->push(); return $this->response()->success('活动已失效')->refresh(); } public function dialog() { $this->confirm('确定使活动失效吗?'); } } return Goods::where('is_pt',1) ->pluck('goods_name','id');