model 设置软删除配置
class Demo extends Model{
use SoftDeletes;
$datas = ['deleted_at'];
#格式化
public function getStartTimeAttribute($value) { return !empty($value)? date('Y-m-d H:i:s', $value):''; } public function setStartTimeAttribute($value) { $this->attributes['start_time'] = !empty($value)? strtotime($value):''; }
}
protected function grid() { // new Grid() $grid = new Grid(new Notice()); // 禁用导出数据按钮 $grid->disableExport(); $grid->filter(function ($filter) { $filter->like('title', '标题'); }); $grid->column('type', '消息类型')->display(function ($type){ return Notice::getType($type); }); $grid->column('title', '标题'); $grid->column('content', '内容')->display(function ($content){ return mb_substr($content,0,20); }); $grid->column('created_at', '发布时间'); $grid->column('created_user_id', '创建人')->display(function ($created_user_id){ return Notice::getUserName($created_user_id); }); $grid->column('start_time', '通知开始时间')->display(function ($start_time){ if(date('Y-m-d',strtotime($start_time))!='1970-01-01'){ return $start_time; } }); $grid->column('end_time', '通知结束时间')->display(function ($end_time){ if(date('Y-m-d',strtotime($end_time))!='1970-01-01'){ return $end_time; } }); $grid->column('updated_at', '修改时间'); $grid->column('updated_user_id', '修改人')->display(function ($updated_user_id){ return Notice::getUserName($updated_user_id); }); $states = [ 'on' => ['text' => 'YES'], 'off' => ['text' => 'NO'], ]; $grid->status('状态')->switch($states); $grid->column('tag', '标签')->display(function ($tag){ return Notice::tranTag($tag); }); $grid->actions(function ($actions) { // 去掉查看 $actions->disableView(); if (!Admin::user()->can('del-customer')) { $actions->disableDelete(); } if (!Admin::user()->can('edit-customer')) { $actions->disableEdit(); } }); $grid->model()->orderBy('id', 'desc'); if (!Admin::user()->can('create-customer')) { $grid->disableCreateButton(); } $grid->tools(function ($tools) { if (!Admin::user()->can('del-customer')) { // 禁用批量删除按钮 $tools->batch(function ($batch) { $batch->disableDelete(); }); } }); return $grid; }
protected function form() { $form = new Form(new Notice); $route_action = Route::currentRouteAction(); $route_action = explode('@', $route_action)[1]; $route_action = strtolower($route_action); $form->hidden('id'); $form->text('title', '标题')->rules('required|min:1|max:50')->placeholder('请输入标题少于50字'); $form->select('type', '类型')->rules('required|min:1')->options(Notice::$type)->placeholder('请选择类型'); $form->datetimeRange('start_time','end_time','启用起止时间'); $form->checkbox('tag', '标签')->options([1 => '新增功能', 2=> '功能优化',3=>'修复BUG']); $form->textarea('content',"内容")->rules('required')->placeholder('少于100字'); $states = [ 'on' => ['text' => 'YES'], 'off' => ['text' => 'NO'], ]; $form->switch('status','状态')->states($states)->default(1); // if ($form->isCreating() || $route_action == 'store') { $form->saving(function (Form $form) { try { $start_time = $form->start_time; $end_time = $form->end_time; if($form->type==2 && (empty($start_time) || empty($end_time))){ $error = new MessageBag(['title' => '失败', 'message' => '启用起止时间 不能为空']); return back()->with(compact('error')); } if($start_time) $start_time = !empty($start_time)? strtotime($start_time):''; if($end_time) $end_time = !empty($end_time)?strtotime($end_time):''; $tag = $form->tag; if(!empty($tag)){ $tag = implode(',',array_filter($tag)); } $content = $form->content; $data = [ 'title' => $form->title, 'type' => $form->type, 'status' => Notice::$map[$form->status]?? 1, // 'status' => $form->status, 'start_time' => $start_time?? '', 'end_time' => $end_time?? '', 'tag' => $tag?? '', 'content' => $content?? '', 'created_user_id' => Admin::user()['id'], 'updated_user_id' => Admin::user()['id'], 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s'), ]; $res = Notice::set($data); admin_toastr(trans('admin.save_succeeded')); return redirect('admin/notice'); } catch (Exception $e) { $error = new MessageBag(['title' => '失败', 'message' => $e->getMessage()]); return back()->with(compact('error')); } }); } elseif ($form->isEditing() || $route_action == 'update') { $form->saving(function (Form $form) { try { $start_time = $form->start_time; $end_time = $form->end_time; if($form->type==2 && (empty($start_time) || empty($end_time))){ $error = new MessageBag(['title' => '失败', 'message' => '启用起止时间 不能为空']); return back()->with(compact('error')); } if($start_time) $start_time = !empty($start_time)? strtotime($start_time):''; if($end_time) $end_time = !empty($end_time)?strtotime($end_time):''; $tag = $form->tag; if(!empty($tag)){ $tag = implode(',',array_filter($tag)); } $content = $form->content; $data = [ 'title' => $form->title, 'type' => $form->type, 'status' => Notice::$map[$form->status]?? 1, 'start_time' => $start_time, 'end_time' => $end_time, 'tag' => $tag, 'content' => $content, 'updated_user_id' => Admin::user()['id'], 'updated_at' => date('Y-m-d H:i:s'), ]; $update = []; foreach ($data as $k=>$v){ if(isset($v)){ $update[$k] = $v; } } $res = Notice::edit($update,$form->model()->id); admin_toastr(trans('admin.save_succeeded')); return redirect('admin/notice'); } catch (Exception $e) { $error = new MessageBag(['title' => '失败', 'message' => $e->getMessage()]); return back()->with(compact('error')); } }); } return $form; }