删除原始类别后如何将未分类类别分配给帖子

时间:2021-05-03 12:26:00

标签: php laravel many-to-many blogs updating

我正在处理一个 Laravel 博客项目,如果我删除一个类别,我希望该类别的所有帖子都移至“未分类”类别。如果帖子属于已删除的类别,其他类别也请避免将该帖子移至未分类,否则将帖子移至未分类。 我正在使用多对多关系作为类别帖子模型。请参阅下面的代码:

迁移后我有

Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->foreignId('user_id')
    ->constrained()
    ->onDelete('cascade');

    $table->string('title')->unique();
    $table->string('featured_image')->nullable()->default('default_featured_image.jpg');
    $table->text('detail');
    $table->string('tags')->nullable();
    $table->timestamps();
});

类别迁移

public function up()
{
    Schema::create('categories', function (Blueprint $table) {
        $table->id();
        $table->string('title');
        $table->text('detail',300)->nullable();
        $table->string('image')->nullable();
        $table->timestamps();
    });
}

关系的枢轴迁移

public function up()
{
    Schema::create('category_post', function (Blueprint $table) {
        $table->id();
        $table->unsignedInteger('post_id');
        $table->unsignedInteger('category_id');
        $table->timestamps();
    });
}

发布模型

 function categories(){
    return $this->belongsToMany(Category::class);
 }   

类别模型

function posts(){
   return $this->belongsToMany(Post::class);
}

类别控制器

public function destroy(Request $request)
{
   $cat_id = $request->category_id;
   $category = Category::find($cat_id);

   $posts_to_update_category_id = $category->posts()->wherePivot('category_id','=',$cat_id)->get();
   foreach($posts_to_update_category_id as $post){
       $post = Post::find($post->id);
       $post->categories()->sync(1); //uncategorized id = 1
   }
   

   $category->delete();
   return redirect('admin/category')->with('success', ''.$category->title.' has been Deleted');
}   

我的代码问题是它最终将所有已删除类别 ID 的帖子更新为 1(未分类)。意思是如果我有 1 个帖子有两个不同的类别,比如 cat1 和 cat2 并且我删除了 cat1,我不希望这篇文章被移动到未分类,但我的代码确实移动了它并从中删除了 cat2 使其只是未分类。如果可以的话请帮忙。谢谢

2 个答案:

答案 0 :(得分:1)

$category->posts()->wherePivot('category_id','=',$cat_id)->get(); 已经返回了帖子实例,因此您不必$post = Post::find($post->id); 以后再

您可以验证它是否有多个类别,这意味着该帖子不应移至未分类,而应删除类别对吗?

所以你可以这样做

public function destroy(Request $request)
{
   $cat_id = $request->category_id;
   $category = Category::find($cat_id);

   $posts = $category->posts()->with('categories')->wherePivot('category_id','=',$cat_id)->get();
   foreach($posts as $post){
       if (count($post->categories) > 1) {
           $post->categories()->detach($cat_id);
       } else {
           $post->categories()->sync(1); //uncategorized id = 1
       }
   }
   

   $category->delete();
   return redirect('admin/category')->with('success', ''.$category->title.' has been Deleted');
}  

答案 1 :(得分:0)

一个简单的解决方案是在删除类别时删除关系

关系的枢轴迁移

public function up()
{
    Schema::create('category_post', function (Blueprint $table) {
        $table->id();
        $table->unsignedInteger('post_id');
        $table->unsignedInteger('category_id');
        $table->timestamps();
        $table->foreignId('category_id')
            ->constrained(categories)
            ->onUpdate('cascade')
            ->onDelete('cascade');
        $table->foreignId('post_id')
            ->constrained(posts)
            ->onUpdate('cascade')
            ->onDelete('cascade');

    });
}

当您删除帖子或类别时,关系将通过级联在数据库中自动删除