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') }