Php 我无法删除数据透视表中的记录

Php 我无法删除数据透视表中的记录,php,sql,laravel,blogs,Php,Sql,Laravel,Blogs,我有五张桌子: 职位 类别 标签 职类职位 邮政标签 我遇到的问题是,如果我删除帖子,那么它也应该删除所有相关表中该帖子的所有关系。但是系统执行的是完全相反的操作,它只是删除post表中的post 我找到了一个解决办法 $table->engine='InnoDB' 但我的问题还是一样 这是我对Category_post Pivot表的迁移 public function up() { Schema::create('post_tag', function (Blueprin

我有五张桌子:

  • 职位
  • 类别
  • 标签
  • 职类职位
  • 邮政标签
我遇到的问题是,如果我删除帖子,那么它也应该删除所有相关表中该帖子的所有关系。但是系统执行的是完全相反的操作,它只是删除post表中的post

我找到了一个解决办法

$table->engine='InnoDB'
但我的问题还是一样

这是我对Category_post Pivot表的迁移

public function up()
{
    Schema::create('post_tag', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->integer('post_id')->index()->unsigned();
        $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
        $table->integer('tag_id')->index()->unsigned();
        $table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');
        $table->timestamps();
    });
}
这就是我在控制器中所做的

public function destroy(Post $post)
{
    $post=Post::find($post->id);
    $post->delete();
    return redirect('admin/post')->with('message','Deleted Sucessfully');
}
我也试过这个

  public function destroy(Post $post)
{
    $post=Post::find($post->id);
    $post->categories()->delete();
    $post->tags()->delete();
    $post->delete();
    return redirect('admin/post')->with('message','Deleted Sucessfully');
}

但是得到了相同的结果

当在Laravel中对许多关系使用透视表时,您应该将关联的标记和类别与Post模型分离,而不是按照
此外,您的控制器代码正在删除标签和类别模型而不是关联,这将损坏附加到这些标签和类别的任何其他帖子。
下面是一个正确方法的示例
在您的
标记中
迁移

/**
*运行迁移。
*
*@返回无效
*/
公共职能
{
Schema::create('tags',函数(Blueprint$table){
$table->bigIncrements('id');
//其他栏目都在这里
$table->timestamps();
});
Schema::create('post_标记',函数(Blueprint$表){
$table->biginger('post_id');
$table->biginger('tag_id');
//确保特定帖子只能与特定标签关联一次
$table->primary(['post_id','tag_id']);
});
}
/**
*反向迁移。
*
*@返回无效
*/
公共职能部门
{
Schema::dropIfExists('post_标记');
Schema::dropIfExists('tags');
}
对类别迁移执行相同的操作
在雄辩的模型中指定
manytomy
关系,如下所示

类后扩展模型
{
公共函数标签()
{
返回$this->belongstomy('App\Tag');
}
公共职能类别()
{
返回$this->belongstomy('App\Category');
}
}
现在,在将标签/类别与帖子关联时,请使用
attach
方法

$post=post::创建([]);//这只是示例代码,请像往常一样填写数据
$tag=tag::create([]);
$category=category::创建([]);
//可以通过模型本身或ID进行附着
$post->tags()->attach($tag);
$post->categories()->attach($categority);
最后,在销毁Post模型时,只需取消与标记和类别的关联,而不是使用
detach
方法删除它们,就像这样

公共功能销毁(Post$Post)
{
$post->categories()->detach();
$post->tags()->detach();
$post->delete();
返回重定向('admin/post')->带有('message','Deleted successfully');
}

在Laravel中对许多关系使用透视表时,应将关联的标记和类别与Post模型分离,而不是按照
此外,您的控制器代码正在删除标签和类别模型而不是关联,这将损坏附加到这些标签和类别的任何其他帖子。
下面是一个正确方法的示例
在您的
标记中
迁移

/**
*运行迁移。
*
*@返回无效
*/
公共职能
{
Schema::create('tags',函数(Blueprint$table){
$table->bigIncrements('id');
//其他栏目都在这里
$table->timestamps();
});
Schema::create('post_标记',函数(Blueprint$表){
$table->biginger('post_id');
$table->biginger('tag_id');
//确保特定帖子只能与特定标签关联一次
$table->primary(['post_id','tag_id']);
});
}
/**
*反向迁移。
*
*@返回无效
*/
公共职能部门
{
Schema::dropIfExists('post_标记');
Schema::dropIfExists('tags');
}
对类别迁移执行相同的操作
在雄辩的模型中指定
manytomy
关系,如下所示

类后扩展模型
{
公共函数标签()
{
返回$this->belongstomy('App\Tag');
}
公共职能类别()
{
返回$this->belongstomy('App\Category');
}
}
现在,在将标签/类别与帖子关联时,请使用
attach
方法

$post=post::创建([]);//这只是示例代码,请像往常一样填写数据
$tag=tag::create([]);
$category=category::创建([]);
//可以通过模型本身或ID进行附着
$post->tags()->attach($tag);
$post->categories()->attach($categority);
最后,在销毁Post模型时,只需取消与标记和类别的关联,而不是使用
detach
方法删除它们,就像这样

公共功能销毁(Post$Post)
{
$post->categories()->detach();
$post->tags()->detach();
$post->delete();
返回重定向('admin/post')->带有('message','Deleted successfully');
}

我只删除透视表中的关系。它与类别和标签无关。因此,分离数据透视表中的值并没有坏处。我只是删除数据透视表中的关系。它与类别和标签无关。因此,分离数据透视表中的值并没有什么害处。