Quick Guide to Laravel Seeds

The Tables

Seeder in Laravel enable you to insert data into your table using only your Code. This way, every time you make a fresh migration you automatically insert the data you defined in your code.

I will show an example here with two tables for a Forum.

In Laravel tables have normally plural names

As we can see we have two tables. The user_id in the posts table is the foreign key of the id in the users table. It's also an one to many Releatonship. One User can have many Posts while the opposite ist not true.

The Seeder

Let's begin with the User. I think Jetstream and Breeze include an UserSeeder. If not then use Artisan to create one.

php artisan make:seeder UserSeeder

As a result, we normally get an empty Seeder. In the Method run() we have to create a blueprint for our User Model by defining the values of the different columns.

namespace Database\Seeders;

use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;

class UserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        User::create([
            'name' => 'admin',
            'email' => 'yourEmailAddress',
            'email_verified_at' => now(),
            'password' => bcrypt('YourPassword'),
            'remember_token' => Str::random(10),
        ]);

        User::create([
            'name' => 'admin2',
            'email' => 'yourEmailAddress',
            'email_verified_at' => now(),
            'password' => bcrypt('YourPassword'),
            'remember_token' => Str::random(10),
        ]);
    }
}

In this Seeder, I included a second Account.

I normally use Seeder when I want to create an Admin Account or if I have a limited amount of Data I want to insert.

If dozens of rows are required for testing than Factories are recommended.

Next would be the PostSeeder which looks like this.

<?php

namespace Database\Seeders;

use App\Models\Post;
use Illuminate\Database\Seeder;

class PostSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Post::create([
            'user_id' => '1',
            'body' => 'This here is a test Post.'
        ]);
    }
}

The user_id here is the foreign key and correspondents to the id of admin, which is created automatically through auto increment.

The DatabaseSeeder

To run our Seeder we actually have to include it in our DatabaseSeeder.php.

<?php

namespace Database\Seeders;



use Illuminate\Database\Seeder;
use App\Models\User;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $this->call(UserSeeder::class);
        $this->call(PostSeeder::class);
    }
}

Now we have to use artisan to run our Seeds.

php artisan db:seed

I personally prefer to make a fresh migration before I seed my Database.

php artisan migrate:fresh --seed

In my next Article I will write about Factories.

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2021 mo-webdesign.com