如何在拉拉维尔(Laravel)中用外键播种单独的桌子

发布于 2025-02-12 11:54:53 字数 1438 浏览 0 评论 0原文

我有三张桌子,带有流派,电影及其关系。我想为关系表创建一个种子,但真的不知道该如何完全做到这一点,以便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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

来世叙缘 2025-02-19 11:54:53

播种您的类型电影最初,您可以使用它们来播种genre_movie表。因此:

// Seed 10 genres
Genere::factory(10)->create();

// Seed 50 movies
Movie::factory(50)->create();

// Seed some relationships
foreach (Genre::all() as $genre) {
    $movies = Movie::inRandomOrder()->take(random_int(1, Movie::count())->get();
    $genre->movies()->attach($movies);
}

更新1

有没有办法控制其将为genre_movie表创建的记录?

是的。

$movies = Movie::inRandomOrder()->take(10)->get();

更改提供给的参数值take(x)将定义每种genre_movie表中创建了多少个记录 genre 。在上面的示例中,它将创建10记录per 流派。 用take(2)等设置为二

可以 genre_movie表中的单个电影 per penre 。

$movies = Movie::inRandomOrder()->first();

Seed your genres and movies tables initially, then you can use those to seed your genre_movie table. So for example:

// Seed 10 genres
Genere::factory(10)->create();

// Seed 50 movies
Movie::factory(50)->create();

// Seed some relationships
foreach (Genre::all() as $genre) {
    $movies = Movie::inRandomOrder()->take(random_int(1, Movie::count())->get();
    $genre->movies()->attach($movies);
}

Update 1

is there a way to control how many records its gonna create for the genre_movie table?

Yup.

$movies = Movie::inRandomOrder()->take(10)->get();

Altering the parameter value provided to take(x) will define how many records are created in the genre_movie table for each Genre. In the above example it will create 10 records per Genre. That could be set to two with take(2) etc.

Alternatively you could always just grab the first() Movie record if you just want a single Movie per Genre in your genre_movie table.

$movies = Movie::inRandomOrder()->first();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文