123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- namespace App\Admin\Controllers;
- use Encore\Admin\Controllers\AdminController;
- use Encore\Admin\Form;
- use Encore\Admin\Grid;
- use Encore\Admin\Show;
- use App\Models\SpecialistCloumn;
- use Illuminate\Http\Request;
- class SpecialistColumnSecondController extends AdminController
- {
- /**
- * Title for current resource.
- *
- * @var string
- */
- protected $title = '健康课堂-二级栏目管理';
- /**
- * Make a grid builder.
- *
- * @return Grid
- */
- protected function grid()
- {
- $grid = new Grid(new SpecialistCloumn);
- $grid->column('id', __('ID'))->sortable();
- $grid->column('pid', __('上级栏目'))->display(function ($pid) {
- return $pid > 0 ? SpecialistCloumn::find($pid)->name : '';
- });
- $grid->column('name', __('名称'));
- $grid->column('is_show', __('是否显示'))->display(function ($isShow) {
- return SpecialistCloumn::$isShowMap[$isShow];
- });
- $grid->column('rank', __('排序(越大越靠前)'))->sortable();
- $grid->column('created_at', __('创建时间'));
- $grid->column('updated_at', __('更新时间'));
- $grid->model()->where('pid', '>', SpecialistCloumn::PID_0);
- //过滤用到的一级栏目
- $cloumns = SpecialistCloumn::where('pid', 0)->get();
- $firstCloumns = [];
- foreach ($cloumns as $cloumn){
- $firstCloumns[$cloumn->id] = $cloumn->name;
- }
- // 过滤
- $grid->filter(function ($filter) use($firstCloumns) {
- $filter->like('name', '名称');
- $filter->equal('pid', '类别')->select($firstCloumns);
- $filter->equal('is_show', '是否显示')->radio(SpecialistCloumn::$isShowMap);
- });
- return $grid;
- }
- /**
- * Make a show builder.
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- $show = new Show(SpecialistCloumn::findOrFail($id));
- $show->field('id', __('ID'));
- $show->field('created_at', __('Created at'));
- $show->field('updated_at', __('Updated at'));
- return $show;
- }
- /**
- * Make a form builder.
- *
- * @return Form
- */
- protected function form()
- {
- $form = new Form(new SpecialistCloumn);
- $form->display('id', __('ID'));
- //只显示一级栏目
- $cloumns = SpecialistCloumn::where('pid', 0)->get();
- $firstCloumns = [];
- foreach ($cloumns as $cloumn){
- $firstCloumns[$cloumn->id] = $cloumn->name;
- }
- $form->select('pid', __('上一级'))->options($firstCloumns)->rules('required');;
- $form->text('name', __('名称'))->rules('required');
- $form->image('pic', __('图片'))->uniqueName()->move('pics/'.date('Y').'/'.date('m'));
- $form->text('url', __('链接地址'));
- $states = [
- 'on' => ['value' => 1, 'text' => '显示', 'color' => 'success'],
- 'off' => ['value' => 0, 'text' => '不显示', 'color' => 'danger'],
- ];
- $form->switch('is_show', __('是否显示'))->states($states);
- $form->number('rank', __('排序(越大越靠前)'))->default(0);
- return $form;
- }
- protected function secondCloumn(Request $request){
- $pid = $request->get('q');
- $result = SpecialistCloumn::where('pid', $pid)->get(['id', 'name as text']);
- return count($result) == 0 ? [['id'=>0,'text'=>'无']] : $result;
- }
- }
|