优化后台的标签和分类设置;优化后台头部菜单;

This commit is contained in:
2022-03-05 21:57:16 +08:00
parent 28cb3bde15
commit 43bf3b0c40
2 changed files with 40 additions and 37 deletions

View File

@@ -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')]));
}