Home > Blockchain >  How to seed a separate table with foreign keys in Laravel
How to seed a separate table with foreign keys in Laravel

Time:07-03

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');
    }
}

CodePudding user response:

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();
  • Related