Home > database >  Laravel Eloquent Migrate and Seed pivot table
Laravel Eloquent Migrate and Seed pivot table

Time:12-10

I just started to learn Laravel and I have a problem with seeding the database with proper relationships.

I tried to create four tables which are: 'Users','Projekts','Posts','Projekt_User_Pivots'.

The idea is that every Projekt can have multiple users, every user can have multiple projekts and posts. So I want to show only the posts within a project, from the users that belong to the project. I tried to seed the database with seeders and factories but I can't achieve the relationship of primarykeys via the factories I want to use. In my solution I just need to run the seeding as often until it works what is realy dumb. I would appreciate some help.

Here are all Models:

Post.php:

<?php

namespace App\Models;

use App\Models\User;
use App\Models\Projekt;
use Illuminate\Database\Eloquent\Model;
use SebastianBergmann\CodeUnit\FunctionUnit;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Post extends Model
{
    use HasFactory;

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

Projekt.php:

<?php

namespace App\Models;

use App\Models\Post;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Projekt extends Model
{
    use HasFactory;

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function projektuserpivot()
    {
        return $this->hasMany(ProjektUserPivot::class);
    }
}

ProjektUserPivot.php:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class ProjektUserPivot extends Model
{
    use HasFactory;

    protected $primaryKey = ['user_id', 'projekt_id'];
    public $incrementing = false;

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function projekt()
    {
        return $this->belongsTo(Projekt::class);
    }

}

User.php:

<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Models\Post;
use App\Models\Projekt;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function post()
    {
        return $this->hasMany(Post::class);
    }

    public function projekt()
    {
        return $this->hasMany(Projekt::class);
    }

    public function projektuserpivot()
    {
        return $this->hasMany(ProjektUserPivot::class);
    }
}

Here are all Factories:

PostFactory.php:

<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Spatie\LaravelIgnition\Support\Composer\FakeComposer;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Post>
 */
class PostFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition()
    {
        return [
            'user_id' => $this->faker->numberBetween(0,999),
            'title' => $this->faker->realTextBetween(10, 50),
            'body' => $this->faker->realTextBetween(800, 1400),
        ];
    }
}

ProjektFactory.php:

<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Projekt>
 */
class ProjektFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition()
    {
        return [
            'name' => $this->faker->realTextBetween(10, 20),
            'user_id' => $this->faker->numberBetween(1,999),
        ];
    }
}

ProjektUserPivotFactory.php:

<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\ProjektUserPivot>
 */
class ProjektUserPivotFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition()
    {
        return [
            'user_id' => $this->faker->numberBetween(1,999),
            'projekt_id' => $this->faker->numberBetween(1,20),
        ];
    }
}

UserFactory.php:

<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User>
 */
class UserFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition()
    {
        return [
            'name' => fake()->name(),
            'email' => fake()->unique()->safeEmail(),
            'email_verified_at' => now(),
            'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
            'remember_token' => Str::random(10),
        ];
    }

    /**
     * Indicate that the model's email address should be unverified.
     *
     * @return static
     */
    public function unverified()
    {
        return $this->state(fn (array $attributes) => [
            'email_verified_at' => null,
        ]);
    }
}

Here are all of the Migrations:

2014_10_12_000000_create_users_table.php:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
};

2022_12_08_154030_create_projekts_table.php:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('projekts', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id');
            $table->string('name');
            $table->timestamps();
            $table->softDeletes();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('projekts');
    }
};

2022_12_08_154042_create_posts_table.php:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id');
            $table->string('title');
            $table->text('body');
            $table->timestamps();
            $table->softDeletes();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
};

2022_12_09_114733_create_projekt_user_pivots_table.php:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('projekt_user_pivots', function (Blueprint $table) {
            $table->unsignedBigInteger('user_id');
            $table->unsignedBigInteger('projekt_id');
            $table->primary(['user_id','projekt_id']);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('projekt_user_pivots');
    }
};

Here are all Seeders:

DatabaseSeeder.php:

<?php

namespace Database\Seeders;

// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use App\Models\Post;
use App\Models\User;
use App\Models\Projekt;
use App\Models\ProjektUserPivot;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {


        User::factory(10)
            ->has(Post::factory()->count(10))
            ->has(Projekt::factory()->count(2))
            ->has(ProjektUserPivot::factory()->count(4))
        ->create();

    }
}

PostSeeder.php:

<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class PostSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //
    }
}

ProjektSeeder.php:

<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class ProjektSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {

    }
}

ProjektUserPivotSeeder.php:

<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class ProjektUserPivotSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //
    }
}

I know why the problem arises but how can I solve it?

Working migration and seeding

Not working migration and seeding

CodePudding user response:

Laravel and its ORM Eloquent work better if you use exactly what they ask for: the pivot tabel between two table must be constructed as the following:

Schema::create('first_second', function(Blueprint $table)
   {
       $table->increments('id');
       $table->integer('first_id');
       $table->integer('second_id');
   });

In your example, your pivot table is named projekt_user_pivot, and the ORM can't access it natively. Plus, Eloquent adds the automatic plural to the table, to it tries to find a table named projekt_user_pivots.

Add the $table->increments('id'); in the migration of your ProjektUser migration, and I suggest to follow Laravel's guidelines. You won't even need to create the model ProjektUser as Eloquent will do it itself.

~~

How to create your pivot table:

Make a migration (php artisan make:migration create_projekt_user_table)

function up() {

   Schema::create('projekt_user', function(Blueprint $table)
       {
           $table->increments('id');
           $table->integer('projekt_id');
           $table->integer('user_id');
       });
}

In your Models :

User.php

public function projekts()
{
    $this->belongsToMany(User::class); 
}

Projekt.php

public function users()
{
    $this->belongsToMany(Projekt::class);
}

And your factories :

UserFactory.php

$projekts = Projekt::factory()->count(3)->create();
 
$user = User::factory()
            ->count(3)
            ->hasAttached($projekts)
            ->create();

This code will create 3 Users and 3 Projects, and all the Users will be linked to the 3 projekts in the pivot table, creating 9 lines.

With the definitions in the models that there is a many to many relationship between User and Projekt, your factory won't have any problem to populate itself the pivot table.

Note: if you ever need to add some additional data inside your pivot table (for example a role for the user for this specific project), you just have to update the migration to add the field, and you can define it with :

User.php

public function projekts()
{
    $this->belongsToMany(User::class)->withPivot('role'); 
}

And you'll be able to access it in you code with :

foreach($user->projekts as $projekt)
{
    echo $projekt->pivot->role; 
}
  • Related