Category : #laravel
Tags : #laravel, #database and migration
Assume we have a table of users
, posts
, and tags
. The relationship between them is a user has many posts and a post has many tags. And there is a pivot table called post_tag
. Now we want to seed data with those tables together. How we can do that?
So if you do not know laravel 9 seed many to many pivot table, then this laravel factory many to many tutorial is going to be for you. In this tutorial, I will show you laravel seed pivot table and laravel factory pivot table with laravel factory create many to many relationship.
Now run the below command to create these three factory like:
php artisan make:factory UserFactory
php artisan make:factory TagFactory
php artisan make:factory PostFactory
Now I am assuming you have a post_tags
pivot table which contains post_id
and tag_id
. Now update the Post model like:
App\Models\Post.php
<?php
namespace App\Models;
use Illuminate\Support\Facades\URL;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class Post extends Model
{
use HasFactory;
protected $fillable = [
'user_id',
'title',
];
public function tags() : BelongsToMany
{
return $this->belongsToMany(Tag::class,'post_tags');
}
}
Read also: Laravel 9 Factory Create With Relationship Example
Now our post model is ready. So update the TagFactory
like:
Database\Factories\TagFactory.php
<?php
namespace Database\Factories;
use App\Models\Tag;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Post>
*/
class TagFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
'title' => fake()->word()
];
}
}
Laravel by default provides UserFactory like:
Database\Factories\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
{
public function definition()
{
return [
'name' => fake()->name(),
'email' => fake()->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi',
'remember_token' => Str::random(10),
];
}
public function unverified()
{
return $this->state(fn (array $attributes) => [
'email_verified_at' => null,
]);
}
}
So now open DatabaseSeeder
class and update it like:
Database\Seeders\DatabaseSeeder.php
<?php
namespace Database\Seeders;
use App\Models\Post;
use App\Models\User;
use App\Models\Tag;
use Illuminate\Database\Seeder;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
class DatabaseSeeder extends Seeder
{
public function run()
{
$tags = Tag::factory(10)->create();
User::factory(20)->create()
->each(function($user) use($tags){
Post::create([
'user_id' => $user->id,
'title' => fake()->paragraph()
])->each(function($post) use($tags){
$post->tags()->attach($tags->random(2));
});
});
}
}
Now if you run the below command:
php artisan db:seed
Now it will seed all your fake data for users
, posts
and tags
and also post_tags
table.
Read also: How To Define IP And MAC Address Column Type In Laravel
Conclusion
I have tried to discuss the clear concept of laravel factory many to many. Now we know laravel 9 factory many to many. Hope this laravel 9 seed many to many pivot table tutorial will help you.