文章关联多分类
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Admin\Controllers\Article;
|
||||
|
||||
use App\Admin\Selectable\CategorySelectAble;
|
||||
use App\Models\Article;
|
||||
use App\Models\Category;
|
||||
use Encore\Admin\Controllers\AdminController;
|
||||
@@ -20,7 +21,7 @@ class IndexController extends AdminController
|
||||
$grid->filter(function ($filter) {
|
||||
$filter->column(1 / 2, function ($filter) {
|
||||
$filter->like('title', '文章标题');
|
||||
$filter->like('category_id', '所属分类')->select(Category::selectOptions(function ($model) {
|
||||
$filter->equal('categories.id', '所属分类')->select(Category::selectOptions(function ($model) {
|
||||
return $model->where('status', 1)->whereIn('type', [Category::TYPE_ARTICLE, Category::TYPE_SHOW]);
|
||||
}, '所有分类'));
|
||||
});
|
||||
@@ -30,7 +31,9 @@ class IndexController extends AdminController
|
||||
|
||||
$grid->column('id', '#ID#');
|
||||
$grid->column('cover', '封面图片')->image('', 100);
|
||||
$grid->column('category.title', '所属分类');
|
||||
$grid->column('所属分类')->display(function () {
|
||||
return $this->categories()->pluck('title');
|
||||
})->label();
|
||||
$grid->column('title', '文章标题');
|
||||
$states = [
|
||||
'on' => ['value' => 1, 'text' => '打开', 'color' => 'primary'],
|
||||
@@ -48,14 +51,8 @@ class IndexController extends AdminController
|
||||
$form = new Form(new Article);
|
||||
|
||||
$form->text('title', '文章标题')->rules('min:2');
|
||||
$form->select('category_id', '所属分类')
|
||||
->options(Category::selectOptions(function ($model) {
|
||||
return $model->where('status', 1)->whereIn('type', [Category::TYPE_ARTICLE, Category::TYPE_SHOW]);
|
||||
}, '选择分类'))
|
||||
->rules('required|min:1', [
|
||||
'required' => '必须选择所属分类',
|
||||
'min' => '必须选择所属分类',
|
||||
]);
|
||||
$form->belongsToMany('categories', CategorySelectAble::class, __('关联分类'));
|
||||
|
||||
$form->textarea('description', '内容简介');
|
||||
$form->image('cover', '封面')
|
||||
->move('images/' . date('Y/m/d'))
|
||||
|
||||
@@ -38,7 +38,7 @@ class IndexController extends AdminController
|
||||
->when('show', function (WidgetsForm $form) {
|
||||
$form->select('article_id', '关联文章')
|
||||
->options(function ($option, $info) {
|
||||
return Article::whereHas('category', function ($q) {
|
||||
return Article::whereHas('categories', function ($q) {
|
||||
$q->where('type', 'show');
|
||||
})->pluck('title', 'id');
|
||||
})->help('当分类类型是文章详情的时候需要选择关联文章');
|
||||
@@ -101,7 +101,7 @@ class IndexController extends AdminController
|
||||
->when('show', function (Form $form) {
|
||||
$form->select('article_id', '关联文章')
|
||||
->options(function ($option, $info) {
|
||||
return Article::whereHas('category', function ($q) {
|
||||
return Article::whereHas('categories', function ($q) {
|
||||
$q->where('type', 'show');
|
||||
})->pluck('title', 'id');
|
||||
})->help('当分类类型是文章详情的时候需要选择关联文章');
|
||||
|
||||
44
app/Admin/Selectable/CategorySelectAble.php
Normal file
44
app/Admin/Selectable/CategorySelectAble.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Admin\Selectable;
|
||||
|
||||
use App\Models\Category;
|
||||
use Encore\Admin\Grid\Filter;
|
||||
use Encore\Admin\Grid\Selectable;
|
||||
|
||||
class CategorySelectAble extends Selectable
|
||||
{
|
||||
|
||||
public $model = Category::class;
|
||||
|
||||
public static function display()
|
||||
{
|
||||
return function ($value) {
|
||||
if (is_array($value)) {
|
||||
return implode(';', array_column($value, 'title'));
|
||||
}
|
||||
|
||||
return optional($this->categories)->title;
|
||||
};
|
||||
}
|
||||
|
||||
public function make()
|
||||
{
|
||||
$this->model()->where('status', 1);
|
||||
|
||||
$this->column('id', 'ID');
|
||||
$this->column('title', '分类名称');
|
||||
$this->column('type', '类型')->using(Category::TYPES);
|
||||
|
||||
$this->filter(function (Filter $filter) {
|
||||
$filter->like('title', '分类名称');
|
||||
$filter->equal('parent.id', '所属分类')
|
||||
->select(Category::selectOptions(function ($model) {
|
||||
return $model->where('status', 1)
|
||||
->whereIn('type', [Category::TYPE_ARTICLE, Category::TYPE_SHOW]);
|
||||
}, '所有分类'));
|
||||
$filter->equal('type', '类型')->select(Category::TYPES);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user