Schema::table('performance', function($table)
{
$table->foreign('song_id')
->references('id')
->on('songs')
->onDelete('cascade');
});
Schema::table('performance', function($table)
{
$talbe->foreign('artist_id')
->references('id')
->on('artists')
->onDelete('cascade');
});
Run Code Online (Sandbox Code Playgroud)
我收到错误:“在非对象上调用成员函数foreign()”。
相反,您不能对外键使用多个命令。您只需要一个外键命令即可组合您想要定义的所有行。这是您应该运行的代码。
Schema::table('performance', function (Blueprint $table)
{
$table->unsignedInteger('song_id');
$table->unsignedInteger('artist_id');
$table->foreign(['song_id','artist_id'])->references(['id','id'])
->on(['song','artists'])->onDelete(['cascade','cascade']);
});
Run Code Online (Sandbox Code Playgroud)
如果不起作用(以前的 Laravel 版本可能不起作用),我们可以使用普通 SQL 语句添加其他外键。
Schema::table('performance', function (Blueprint $table)
{
$table->unsignedInteger('song_id');
$table->unsignedInteger('artist_id');
$table->foreign('song_id')->references('id')
->on('song')->onDelete('cascade');
});
DB::statement(
"ALTER TABLE performance ADD FOREIGN KEY (artist_id) REFERENCES artists(id) ON DELETE CASCADE"
);
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你!
| 归档时间: |
|
| 查看次数: |
7113 次 |
| 最近记录: |