Php 删除未分类的帖子后,如何将其分配给原始类别
我正在从事一个laravel博客项目,如果我删除了一个类别,我希望这个类别的所有文章都转移到“未分类”类别。如果文章属于已删除的类别和其他类别,则避免将该文章移至未分类,否则将该文章移至未分类。 我使用多对多的分类后模型的关系。请参见下面的我的代码: 迁移后我有Php 删除未分类的帖子后,如何将其分配给原始类别,php,laravel,many-to-many,blogs,updating,Php,Laravel,Many To Many,Blogs,Updating,我正在从事一个laravel博客项目,如果我删除了一个类别,我希望这个类别的所有文章都转移到“未分类”类别。如果文章属于已删除的类别和其他类别,则避免将该文章移至未分类,否则将该文章移至未分类。 我使用多对多的分类后模型的关系。请参见下面的我的代码: 迁移后我有 Schema::create('posts', function (Blueprint $table) { $table->id(); $table->foreignId('user_id') -&g
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();
});
}
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');
});
}
后期模型
function categories(){
return $this->belongsToMany(Category::class);
}
function posts(){
return $this->belongsToMany(Post::class);
}
类别模型
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(未分类)。也就是说,如果我有一篇文章有两个不同的类别,比如cat1和cat2,我删除cat1,我不希望这篇文章被移动到未分类的位置,但是我的代码确实移动了它,并且从中删除了cat2,使它只是未分类。如果可以的话,请帮忙。谢谢$category->posts()->wherePivot('category_id','=',$cat_id)->get()
已经返回posts实例,因此您不必执行$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');
}
一个简单的解决方案是在删除类别时删除关系
关系的轴心迁移
public function up()
{
Schema::create('category_post', function (Blueprint $table) {
$table->id();
$table->unsignedInteger('post_id');
$table->unsignedInteger('category_id');
$table->timestamps();
});
}
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');
});
}
当您删除帖子或类别时,关系将被数据库中的cascade Automatically删除为什么要创建一个名为未分类的类别
,只需将其设置为当它没有类别时,它是未分类的。非常感谢您的帮助me@Richie很高兴它成功了,如果可以的话,把我的答案标记为正确,这样其他有同样问题的人就可以看到什么对你有用了。非常感谢much@Richie如果答案对您有帮助,请将其设置为“正确答案”,这样您的问题将被标记为“已结束”