如何在拉拉维尔(Laravel)中用外键播种单独的桌子
我有三张桌子,带有流派,电影及其关系。我想为关系表创建一个种子,但真的不知道该如何完全做到这一点,以便GenRE_ID和Movie_ID具有相同的数字ID。这是流派的迁移:
public function up()
{
Schema::create('genres', function (Blueprint $table) {
$table->id();
$table->string('name');
});
}
电影:
public function up()
{
Schema::create('movies', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->integer('status')->nullable()->default(0);
$table->string('image_path')->default('images/default.png');
});
}
关系:
public function up()
{
Schema::create('genre_movie', function (Blueprint $table) {
$table->foreignId('genre_id')->constrained();
$table->foreignId('movie_id')->constrained();
});
}
这是电影模型:
class Movie extends Model
{
use HasFactory;
protected $fillable = ['name', 'status', 'image_path'];
public function genres()
{
return $this->belongsToMany(Menu::class, 'genre_movie');
}
}
这是流派模型:
class Genre extends Model
{
use HasFactory;
protected $fillable = ['name'];
public function movies()
{
return $this->belongsToMany(Movie::class, 'genre_movie');
}
}
I have three tables with genres, movies and their relationship. I want to create a seed for the relationship table but don't really know how to do it exactly so the genre_id and movie_id would have the same number id. Here's the migration for the genres:
public function up()
{
Schema::create('genres', function (Blueprint $table) {
$table->id();
$table->string('name');
});
}
Movies:
public function up()
{
Schema::create('movies', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->integer('status')->nullable()->default(0);
$table->string('image_path')->default('images/default.png');
});
}
Relationship:
public function up()
{
Schema::create('genre_movie', function (Blueprint $table) {
$table->foreignId('genre_id')->constrained();
$table->foreignId('movie_id')->constrained();
});
}
Here's the movie model:
class Movie extends Model
{
use HasFactory;
protected $fillable = ['name', 'status', 'image_path'];
public function genres()
{
return $this->belongsToMany(Menu::class, 'genre_movie');
}
}
Here's the genre model:
class Genre extends Model
{
use HasFactory;
protected $fillable = ['name'];
public function movies()
{
return $this->belongsToMany(Movie::class, 'genre_movie');
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
播种您的
类型
和电影
最初,您可以使用它们来播种genre_movie
表。因此:更新1
是的。
更改提供给
的参数值take(x)
将定义每种genre_movie
表中创建了多少个记录 genre 。在上面的示例中,它将创建10
记录per流派
。 用take(2)
等设置为二可以
genre_movie
表中的单个电影
per penre 。Seed your
genres
andmovies
tables initially, then you can use those to seed yourgenre_movie
table. So for example:Update 1
Yup.
Altering the parameter value provided to
take(x)
will define how many records are created in thegenre_movie
table for eachGenre
. In the above example it will create10
records perGenre
. That could be set to two withtake(2)
etc.Alternatively you could always just grab the
first()
Movie
record if you just want a singleMovie
perGenre
in yourgenre_movie
table.