From 1f6ddc19930b123d39d87740bfdd87e6167eb60c Mon Sep 17 00:00:00 2001 From: xuanchen <122383162@qq.com> Date: Wed, 16 Sep 2020 08:36:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=90=8E=E5=8F=B0=E5=88=86?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Category/IndexController.php | 68 +++++++++++-------- 1 file changed, 38 insertions(+), 30 deletions(-) diff --git a/app/Admin/Controllers/Category/IndexController.php b/app/Admin/Controllers/Category/IndexController.php index ab1198c..0305b38 100644 --- a/app/Admin/Controllers/Category/IndexController.php +++ b/app/Admin/Controllers/Category/IndexController.php @@ -34,15 +34,24 @@ class IndexController extends AdminController }, '一级分类')); $form->text('title', '分类名称')->rules('required'); $form->select('type', '分类类型') - ->options(Category::TYPES) - ->required(); + ->options(Category::TYPES) + ->when('show', function (WidgetsForm $form) { + $form->select('article_id', '关联文章') + ->options(function ($option, $info) { + return Article::whereHas('category', function ($q) { + $q->where('type', 'show'); + })->pluck('title', 'id'); + })->help('当分类类型是文章详情的时候需要选择关联文章'); + }) + ->required(); $form->textarea('description', '分类简介') - ->rules('nullable'); + ->rules('nullable'); $form->image('cover', 'Logo') - ->move('images/' . date('Y/m/d')) - ->removable() - ->uniqueName(); + ->move('images/' . date('Y/m/d')) + ->removable() + ->uniqueName(); $form->number('order', '排序')->default(0); + $form->switch('top_show', '顶部导航显示')->states()->default(0); $form->switch('status', '显示')->states()->default(1); $form->action(admin_url('categories')); @@ -88,37 +97,36 @@ class IndexController extends AdminController }, '一级分类')); $form->text('title', '分类名称')->rules('required'); $form->select('type', '分类类型') - ->options(Category::TYPES) - ->required() - ->rules('required'); + ->options(Category::TYPES) + ->when('show', function (Form $form) { + $form->select('article_id', '关联文章') + ->options(function ($option, $info) { + return Article::whereHas('category', function ($q) { + $q->where('type', 'show'); + })->pluck('title', 'id'); + })->help('当分类类型是文章详情的时候需要选择关联文章'); + }) + ->required() + ->rules('required'); $form->textarea('description', '分类简介')->rows(4)->rules('nullable'); $form->image('cover', 'Logo') - ->move('images/' . date('Y/m/d')) - ->removable() - ->uniqueName(); + ->move('images/' . date('Y/m/d')) + ->removable() + ->uniqueName(); $form->number('order', '排序')->default(0)->help('正序优先'); - $form->select('article_id', '关联文章') - ->options(function ($option, $info) { - $category = $this; - if ($category) { - return Article::where('category_id', $category->id)->pluck('title', 'id'); - } else { - return [0 => '没有数据']; - } - })->help('当分类类型是文章详情的时候需要选择关联文章'); - + $form->switch('top_show', '顶部导航显示')->states()->default(0); $form->switch('status', '显示')->states()->default(1); $form->saving(function (Form $form) { if (request()->has('title')) { - if (request()->type == Category::TYPE_SHOW && empty(request()->article_id)) { - $error = new MessageBag([ - 'title' => '错误', - 'message' => '文章类型是文章详情的时候需要选择关联文章', - ]); - - return back()->withInput()->with(compact('error')); - } + // if (request()->type == Category::TYPE_SHOW && empty(request()->article_id)) { + // $error = new MessageBag([ + // 'title' => '错误', + // 'message' => '文章类型是文章详情的时候需要选择关联文章', + // ]); + // + // return back()->withInput()->with(compact('error')); + // } } });