123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
- namespace App\Admin\Controllers;
- use App\Article;
- use App\Articletype;
- use App\Http\Controllers\Controller;
- use Encore\Admin\Controllers\HasResourceActions;
- use Encore\Admin\Form;
- use Encore\Admin\Grid;
- use Encore\Admin\Layout\Content;
- use Encore\Admin\Show;
- class ArticleController 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('文章管理')
- ->description('详情')
- ->body($this->detail($id));
- }
- /**
- * Edit interface.
- *
- * @param mixed $id
- * @param Content $content
- * @return Content
- */
- public function edit($id, Content $content)
- {
- return $content
- ->header('文章管理')
- ->description('编辑')
- ->body($this->form()->edit($id));
- }
- /**
- * Create interface.
- *
- * @param Content $content
- * @return Content
- */
- public function create(Content $content)
- {
- return $content
- ->header('文章管理')
- ->description('创建')
- ->body($this->form());
- }
- /**
- * Make a grid builder.
- *
- * @return Grid
- */
- protected function grid()
- {
- $grid = new Grid(new Article);
- $grid->id('编号');
- $grid->name('标题');
- $grid->articletype_id('文章类型')->display(function ($tid) {
- return Articletype::where('id', $tid)->value('name') ?? 未分类;
- })->label();
- $grid->source('来源');
- $grid->author('作者');
- $grid->sort('排序')->editable();
- $grid->ispush("是否发布")->display(function ($push) {
- if ($push) {
- return "已发布";
- }
- return "未发布";
- })->label();
- $grid->updated_at('更新时间');
- $grid->filter(function ($filter) {
- // 去掉默认的id过滤器
- $filter->disableIdFilter();
- // 在这里添加字段过滤器
- $filter->like('name', '标题');
- });
- $grid->disableExport();
- $grid->actions(function (Grid\Displayers\Actions $actions) {
- $actions->disableView();
- });
- return $grid;
- }
- /**
- * Make a show builder.
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- $show = new Show(Article::findOrFail($id));
- $show->id('编号');
- $show->name('标题');
- $show->picture();
- $show->content('内容')->unescape();
- $show->articletype_id('文章类型')->as(function ($tid) {
- $name = Articletype::where('id', $tid)->select('name')->first();
- if ($name) {
- return $name['name'];
- }
- return "未分类";
- });
- $show->source('来源');
- $show->author('作者');
- $show->ispush('是否发布')->as(function ($ispush) {
- if ($ispush) {
- return "已发布";
- }
- return "未发布";
- });
- $show->created_at('创建时间');
- $show->updated_at('更新时间');
- return $show;
- }
- /**
- * Make a form builder.
- *
- * @return Form
- */
- protected function form()
- {
- $form = new Form(new Article);
- $form->text('name', '文章名称')->rules('required');
- $form->image('picture', '文章封面')->uniqueName()->rules('required')->help('图片尺寸:172px * 115px');
- $form->editor('content', '内容')->rules('required');
- $form->select('articletype_id', '文章类型')->options('/admin/api/getArticleTypeApi')->rules('required');
- $form->text('author', '作者')->rules('required');
- $form->text('source', '来源');
- $form->number('sort', '排序')->help('注:数值越大越靠前');
- $states = [
- 'on' => ['value' => 1, 'text' => '已发布', 'color' => 'success'],
- 'off' => ['value' => 0, 'text' => '未发布', 'color' => 'danger'],
- ];
- $form->switch("ispush", "发布状态")->states($states)->default('1');
- $form->disableCreatingCheck();
- $form->disableEditingCheck();
- $form->disableViewCheck();
- return $form;
- }
- }
|