我的迁移文件
public function up()
{
Schema::table('chatter_discussion', function (Blueprint $table) {
$table->integer('chatter_category_id')->unsigned()->index();
$table->foreign('chatter_category_id')->references('id')->on('chatter_categories')
->onDelete('cascade')
->onUpdate('cascade');
$table->bigInteger('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users')
->onDelete('cascade')
->onUpdate('cascade');
});
Schema::table('chatter_post', function (Blueprint $table) {
$table->integer('chatter_discussion_id')->unsigned()->index();
$table->foreign('chatter_discussion_id')->references('id')->on('chatter_discussion')
->onDelete('cascade')
->onUpdate('cascade');
$table->bigInteger('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users')
->onDelete('cascade')
->onUpdate('cascade');
});
}
答案 0 :(得分:1)
您只需在模型中定义关系,而无需设置外键等。
https://laravel.com/docs/5.8/eloquent-relationships
因此,在您的ChatterDiscussion / ChatterPost模型(可能必须创建)中,您将具有以下功能:
public function chatter_category()
{
return $this->hasOne('App\ChatterCategory');
}
在ChatterCategory(可能还需要创建)中,您将得到相反的结果:
public function chatter_discussion()
{
return $this->belongsTo('App\ChatterDiscussion');
}
public function chatter_post()
{
return $this->belongsTo('App\ChatterPost');
}
然后,您可以在模型中的每个删除功能中处理删除。
无论哪种方式,您的错误都是您两次创建相同的列名。这样的事情应该可以工作(我还没有测试过)
Schema::table('chatter_discussion', function (Blueprint $table) {
$table->foreign('chatter_category_id')->unsigned()->index()->references('id')->on('chatter_categories')
->onDelete('cascade')
->onUpdate('cascade');
$table->foreign('user_id')->references('id')->on('users')
->onDelete('cascade')
->onUpdate('cascade');
});
Schema::table('chatter_post', function (Blueprint $table) {
$table->foreign('chatter_discussion_id')->unsigned()->index()->references('id')->on('chatter_discussion')
->onDelete('cascade')
->onUpdate('cascade');
$table->foreign('user_id')->references('id')->on('users')
->onDelete('cascade')
->onUpdate('cascade');
});