如何在删除原始类别后将未分类的类别分配给 post
How do I assign uncategorized category to a post after deleting it original category
我正在开发一个 laravel 博客项目,如果我删除一个类别,我希望该类别的所有 post 移动到 'Uncategorized' 类别。如果 post 属于已删除的类别和其他类别,则避免将 post 移至未分类,否则将 post 移至未分类。
我正在为 Category-Post 模型使用多对多关系。请参阅下面的代码:
Post 我有迁移
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();
});
}
Post 型号
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');
}
我的代码问题是它最终将所有 post 与已删除的类别 ID 更新为 1(未分类)。意思是如果我有 1 个 post 有两个不同的类别,比如 cat1 和 cat2,我删除了 cat1,我不希望这个 post 被移动到未分类,但我的代码确实移动了它并且也从中删除了 cat2使其只是未分类。如果可以的话请帮忙。谢谢
$category->posts()->wherePivot('category_id','=',$cat_id)->get();
已经有 returns 个 post 实例,所以您以后 $post = Post::find($post->id);
不必再做
您可以验证它是否有多个类别,这意味着此 post 不应移至未分类,而只是删除类别对吗?
所以你可以这样做
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');
}
一个简单的解决方案是在删除类别时删除关系
关系的枢轴迁移
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');
});
}
当您删除 post 或类别时,关系将被数据库中的级联自动删除
我正在开发一个 laravel 博客项目,如果我删除一个类别,我希望该类别的所有 post 移动到 'Uncategorized' 类别。如果 post 属于已删除的类别和其他类别,则避免将 post 移至未分类,否则将 post 移至未分类。 我正在为 Category-Post 模型使用多对多关系。请参阅下面的代码:
Post 我有迁移
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();
});
}
Post 型号
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');
}
我的代码问题是它最终将所有 post 与已删除的类别 ID 更新为 1(未分类)。意思是如果我有 1 个 post 有两个不同的类别,比如 cat1 和 cat2,我删除了 cat1,我不希望这个 post 被移动到未分类,但我的代码确实移动了它并且也从中删除了 cat2使其只是未分类。如果可以的话请帮忙。谢谢
$category->posts()->wherePivot('category_id','=',$cat_id)->get();
已经有 returns 个 post 实例,所以您以后 $post = Post::find($post->id);
不必再做
您可以验证它是否有多个类别,这意味着此 post 不应移至未分类,而只是删除类别对吗?
所以你可以这样做
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');
}
一个简单的解决方案是在删除类别时删除关系
关系的枢轴迁移
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');
});
}
当您删除 post 或类别时,关系将被数据库中的级联自动删除