147 lines
4.0 KiB
PHP
147 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Controllers;
|
|
|
|
use App\Models\Article;
|
|
use App\Models\Category;
|
|
use Encore\Admin\Controllers\AdminController;
|
|
use Encore\Admin\Form;
|
|
use Encore\Admin\Grid;
|
|
use Encore\Admin\Layout\Content;
|
|
use Encore\Admin\Show;
|
|
|
|
class ArticleController extends AdminController
|
|
{
|
|
|
|
/**
|
|
* Index interface.
|
|
*
|
|
* @param Content $content
|
|
* @return Content
|
|
*/
|
|
public function index(Content $content)
|
|
{
|
|
return $content
|
|
->header('资讯管理')
|
|
->description('列表')
|
|
->body($this->grid());
|
|
}
|
|
|
|
/**
|
|
* 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('ID');
|
|
$grid->title('标题');
|
|
$grid->column('category.title', '所属分类');
|
|
$grid->description('简介')->display(function ($text) {
|
|
return str_limit($text, 100, '...');
|
|
});
|
|
$grid->cover('标题图')->image('', 60, 60);
|
|
$grid->status('状态')->switch([
|
|
'on' => ['value' => 1, 'text' => '正常', 'color' => 'primary'],
|
|
'off' => ['value' => 0, 'text' => '关闭', 'color' => 'danger'],
|
|
]);
|
|
|
|
$grid->hot('推荐')->switch([
|
|
'on' => ['value' => 1, 'text' => '是', 'color' => 'primary'],
|
|
'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
|
|
]);
|
|
$grid->order('排序');
|
|
|
|
$grid->filter(function ($filter) {
|
|
// 去掉默认的id过滤器
|
|
$filter->disableIdFilter();
|
|
// 在这里添加字段过滤器
|
|
$filter->like('title', '标题');
|
|
$filter->equal('category.id', '所属分类')->select(Category::selectOptions(function ($model) {
|
|
return $model->where('status', 1)->where('type', 'article');
|
|
}, '所有分类'));
|
|
});
|
|
return $grid;
|
|
}
|
|
|
|
/**
|
|
* Make a show builder.
|
|
*
|
|
* @param mixed $id
|
|
* @return Show
|
|
*/
|
|
protected function detail($id)
|
|
{
|
|
$show = new Show(Article::findOrFail($id));
|
|
|
|
$show->id('ID');
|
|
$show->title('标题');
|
|
$show->description('简介');
|
|
$show->cover('标题图')->image('', 60, 60)->uniqueName();
|
|
$show->content('详情');
|
|
$show->status('状态')->using(['1' => '正常', '0' => '关闭']);
|
|
$show->order('排序');
|
|
$show->clicks('浏览量');
|
|
|
|
return $show;
|
|
}
|
|
|
|
/**
|
|
* Make a form builder.
|
|
*
|
|
* @return Form
|
|
*/
|
|
protected function form()
|
|
{
|
|
$form = new Form(new Article);
|
|
|
|
$form->text('title', '标题')->rules('required');
|
|
|
|
$form->select('category_id', '所属分类')->options(Category::selectOptions(function ($model) {
|
|
return $model->where('status', 1)->where('type', 'article');
|
|
}, '选择分类'))->rules('required', ['required' => '必须选择分类']);
|
|
|
|
$form->textarea('description', '简介')->rules('max:255', ['max' => '简介最多255字']);
|
|
$form->image('cover', '标题图');
|
|
$form->editor('content', '详情')->rules('required', ['required' => '详情不能为空']);
|
|
$form->switch('status', '状态')->default(1);
|
|
$form->switch('hot', '推荐')->default(0);
|
|
$form->number('order', '排序')->default(0);
|
|
$form->number('clicks', '浏览量')->default(0);
|
|
|
|
return $form;
|
|
}
|
|
|
|
}
|