我遇到了一個問題,我已經為 3 個表做了模型和遷移:movies、actors 和 actor_movie (pivot)
當我從模型 Actor 方法 movie() 使用它時,它可以作業,但不能使用 actor() 從 Movie 中作業

class Movie extends Model
{
use HasFactory;
public function actors()
{
return $this->belongsToMany(Actor::class, 'actor_movie');
}
}
class Actor extends Model
{
use HasFactory;
public function movies()
{
return $this->belongsToMany(Movie::class, 'actor_movie');
}
}
電影遷移:
public function up()
{
Schema::create('movies', function (Blueprint $table) {
$table->id();
$table->timestamps();
});
}
演員遷移:
public function up()
{
Schema::create('actors', function (Blueprint $table) {
$table->id();
$table->timestamps();
});
}
樞軸遷移:
public function up()
{
Schema::create('actor_movie', function (Blueprint $table) {
$table->bigInteger('movie_id')->unsigned()->index();
$table->foreign('movie_id')->references('id')->on('movies')->onDelete('cascade');
$table->bigInteger('actor_id')->unsigned()->index();
$table->foreign('actor_id')->references('id')->on('actors')->onDelete('cascade');
$table->primary(['actor_id', 'movie_id']);
$table->timestamps();
});
}
uj5u.com熱心網友回復:
正如評論中所說,您正在 Tinker 控制臺中運行測驗。Tinker 控制臺會在啟動時加載所有 PHP 檔案和依賴項,并將它們保存在記憶體中。
要重繪 您的代碼,您需要殺死 tinker 并重新啟動它
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/349328.html
