当前位置: 首页 > 工具软件 > laravel-love > 使用案例 >

LaravelAdmin-demo/laravel-admin

秦俊豪
2023-12-01

 

 

<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Subject;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class SubjectController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('管理')
            ->description('故事')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed   $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed   $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Subject());

        $grid->id('id')->sortable();
        $grid->title('标题');
        $grid->column('pay_type','支付类型')->display(function ($column) {
            if($column==Subject::PAY_TYPE['no_money']) $return = '免费';
            if($column==Subject::PAY_TYPE['money']) $return = '付费';
            if($column==Subject::PAY_TYPE['appointment']) $return = '预约';
            return $return;
        });
        $grid->column('video_type','视频类型')->display(function ($column) {
            if($column==Subject::VIDEO_TYPE['figure']) $return = '身材';
            if($column== Subject::VIDEO_TYPE['yoga']) $return = '瑜伽';
            if($column==Subject::VIDEO_TYPE['love']) $return = '爱情';
            return $return;
        });
        $grid->column('status','状态')->display(function ($column) {
            $return = $column==Subject::STATUS['upper']?'上架':'下架';
            return $return;
        });
        $grid->column('price','价格');
        $grid->column('image','图片')->display(function ($column) {
            $return = !empty($column)?"https://".env('OSS_BUCKET').'.'.env('OSS_ENDPOINT').'/'.$column:'';
            return $return;
        })->image();
        $grid->column('path','地址')->display(function ($column) {
            $return = !empty($column)?"https://".env('OSS_BUCKET').'.'.env('OSS_ENDPOINT').'/'.$column:'';
            return $return;
        })->link();
        $grid->column('created_at','创建时间');
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 设置created_at字段的范围查询
            $filter->between('created_at', '创建时间')->datetime();
            $filter->equal('status', '课程状态')->select(Subject::STATUS_TXT);
            $filter->equal('pay_type', '付费类型')->select(Subject::PAY_TYPE_TXT);
        });
        //禁用操作栏的修改和删除
        $grid->actions(function ($actions) {
            //$actions->disableView();
            //$actions->disableDelete();
            //$actions->disableEdit();
        });
        //禁用创建按钮
        //$grid->disableCreateButton();
        //禁用分页条
        //$grid->disablePagination();
        //禁用查询过滤器
        // $grid->disableFilter();
        //禁用导出数据按钮
        //$grid->disableExport();
        //禁用行选择checkbox
        //$grid->disableRowSelector();
        //禁用行操作列
        //$grid->disableActions();
        //设置分页选择器选项
        //$grid->perPages([10, 20, 30, 40, 50]);
        $grid->paginate(20);
        //关闭批量删除
        $grid->tools(function ($tools) {
            $tools->batch(function ($batch) {
                $batch->disableDelete();
                // 去掉`查看`按钮
            });
        });



        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed   $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Subject::findOrFail($id));

        $show->id('ID');
        $show->title('title');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        $show->panel()->tools(function ($tools){
            //$tools->disableDelete();
            //$tools->disableEdit();
            //$tools->disableList();
        });
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Subject);

        $form->display('id', 'ID');
        $form->text('title', '名称');
        $form->select('pay_type', '选择付费类型')->options([
            Subject::PAY_TYPE['no_money'] => '免费',
            Subject::PAY_TYPE['money'] => '付费',
            Subject::PAY_TYPE['appointment'] => '预约',
        ])->rules('required')->default(Subject::PAY_TYPE['no_money']);
        $form->select('video_type', '选择视频类型')->options([
            Subject::VIDEO_TYPE['figure'] => '身材',
            Subject::VIDEO_TYPE['yoga'] => '瑜伽',
            Subject::VIDEO_TYPE['love'] => '爱情',
        ])->rules('required')->default(Subject::VIDEO_TYPE['figure']);
        $states = [
            'on'  => ['value' =>  Subject::STATUS['upper'], 'text' => '上架', 'color' => 'success'],
            'off' => ['value' =>  Subject::STATUS['down'], 'text' => '下架', 'color' => 'danger'],
        ];
        $form->switch('status', '选择状态')->states($states)->default('on');
        $form->number('price', '价格')->rules('required');
        //$form->text('path', '视频地址')->rules('required');
        $form->file('path', '文件上传')->move('/upload/video');
        $form->image('image', '图片上传')->move('/upload/image');
        $form->textarea('content','描述')->default('')->rows(10);

        return $form;
    }
}

 

 类似资料: