From 43bf3b0c40f1393b4cd02ec9b9cbcf5e9be7f9d2 Mon Sep 17 00:00:00 2001 From: augushong Date: Sat, 5 Mar 2022 21:57:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=90=8E=E5=8F=B0=E7=9A=84?= =?UTF-8?q?=E6=A0=87=E7=AD=BE=E5=92=8C=E5=88=86=E7=B1=BB=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?;=E4=BC=98=E5=8C=96=E5=90=8E=E5=8F=B0=E5=A4=B4=E9=83=A8?= =?UTF-8?q?=E8=8F=9C=E5=8D=95;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/controller/Post.php | 76 ++++++++++++++++++---------------- view/admin/common/_header.html | 1 - 2 files changed, 40 insertions(+), 37 deletions(-) diff --git a/app/admin/controller/Post.php b/app/admin/controller/Post.php index 5335217..a8f0653 100644 --- a/app/admin/controller/Post.php +++ b/app/admin/controller/Post.php @@ -145,52 +145,56 @@ class Post extends Common if (isset($post_data['categorys'])) { $categorys = $post_data['categorys']; unset($post_data['categorys']); + + $old_category_list = PostCategory::where('post_id', $id)->select(); + $old_category_id_list = array_column((array)$old_category_list, 'id'); + + // 旧的有新的没有 + foreach ($old_category_list as $model_category) { + if (!in_array($model_category->id, $categorys)) { + $model_category->delete(); + } + } + + // 旧的没有新的有 + foreach ($categorys as $category) { + if (!in_array($category, $old_category_id_list)) { + + PostCategory::create([ + 'post_id' => $model_post->id, + 'category_id' => $category + ]); + } + } } if (isset($post_data['tags'])) { $tags = $post_data['tags']; unset($post_data['tags']); + + $old_tag_list = PostTag::where('post_id', $id)->select(); + $old_tag_id_list = array_column((array)$old_tag_list, 'id'); + + foreach ($old_tag_list as $model_tag) { + if (!in_array($model_tag->id, $tags)) { + $model_tag->delete(); + } + } + + foreach ($tags as $tag) { + if (!in_array($tag, $old_tag_id_list)) { + + PostTag::create([ + 'post_id' => $model_post->id, + 'tag_id' => $tag + ]); + } + } } $model_post->save($post_data); - $old_category_list = PostCategory::where('post_id', $id)->select(); - $old_category_id_list = array_column((array)$old_category_list, 'id'); - $old_tag_list = PostTag::where('post_id', $id)->select(); - $old_tag_id_list = array_column((array)$old_tag_list, 'id'); - - // 旧的有新的没有 - foreach ($old_category_list as $model_category) { - if (!in_array($model_category->id, $categorys)) { - $model_category->delete(); - } - } - foreach ($old_tag_list as $model_tag) { - if (!in_array($model_tag->id, $tags)) { - $model_tag->delete(); - } - } - // 旧的没有新的有 - foreach ($categorys as $category) { - if (!in_array($category, $old_category_id_list)) { - - PostCategory::create([ - 'post_id' => $model_post->id, - 'category_id' => $category - ]); - } - } - - foreach ($tags as $tag) { - if (!in_array($tag, $old_tag_id_list)) { - - PostTag::create([ - 'post_id' => $model_post->id, - 'tag_id' => $tag - ]); - } - } return $this->success('保存成功', url('index', ['type' => $model_post->getData('type')])); } diff --git a/view/admin/common/_header.html b/view/admin/common/_header.html index edd958a..8812cda 100644 --- a/view/admin/common/_header.html +++ b/view/admin/common/_header.html @@ -8,7 +8,6 @@ {if check_permission('file_list') }
  • 文件管理
  • {/if} -
  • 内容管理
  • 头条资讯管理
  • 管理员管理