Laravel Data Seeding

Creating a simple seeder

You’ll find your seeder files under database > seeders

Create a new one with this:

php artisan make:seeder UsersTableSeeder

Open new file and add to run function:

        DB::table('users')->insert([
            'name' => Str::random(10),
            'email' => Str::random(10).'@codingfaculty.com',
            'password' => bcrypt('secret')
        ]);

At the top of document add:

use DB;
use Str;

Now run:

php artisan db:seed

Now look at your database to see new user created.

Creating a more advanced seeder with factories

Go to database > factories to see the default one for users. We’ll use this one inside our DatabaseSeeder.php to create many users at once. Under run function:

<?php
namespace Database\Seeders;

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

class DatabaseSeeder extends Seeder
{
    public function run()
    {
        User::factory()->count(10)->create(); 
    }
}

Then run:

php artisan db:seed

Check your database and you’ll see 10 new users with real names.

Create Factories for all Database Tables

Edit user factory:

    public function definition()
    {
        return [
            'name' => fake()->name(),
            'email' => fake()->safeEmail(),
            'email_verified_at' => now(),
            'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
            'remember_token' => Str::random(10),
            'country_id' => fake()->numberBetween(1,3),
        ];
    }
php artisan make:factory PostFactory

Inside new factory add to definition function:

    public function definition()
    {
        return [
            'user_id' => 1,
            'title' => fake()->sentence(7,11),
            'content' => fake()->paragraphs(rand(10,1),true),
            'path' => fake()->randomElement(['first.jpg','second.jpg','third.jpg']),
        ];
    }

Create one for Role:

php artisan make:factory RoleFactory
    public function definition()
    {
        return [
            'name' => fake()->randomElement(['administrator','author','subscriber']),
        ];
    }

Create one for photo:

php artisan make:factory PhotoFactory
    public function definition()
    {
        return [
            'path' => 'placeholder.jpg',
            'imageable_id' => fake()->numberBetween(1,3),
            'imageable_type' => fake()->randomElement(['App\Models\User','App\Models\Post']),
        ];
    }

Create one for country:

php artisan make:factory CountryFactory
    public function definition()
    {
        return [
            'name' => fake()->randomElement(['Canada','United States','United Kingdom']),
        ];
    }

Add to DatabaseSeeder.php:

<?php
namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\User;
use App\Models\Post;
use App\Models\Role;
use App\Models\Photo;
use App\Models\Country;
use DB;

class DatabaseSeeder extends Seeder
{
    public function run()
    {
        DB::statement('SET FOREIGN_KEY_CHECKS=0');
        DB::table('users')->truncate();
        DB::table('posts')->truncate();
        DB::table('roles')->truncate();
        DB::table('photos')->truncate();
        DB::table('countries')->truncate();

        User::factory()
            ->count(3)
            ->has(Post::factory()->count(2)->has(Photo::factory()->count(1)))
            ->has(Photo::factory()->count(1))
            ->create();
        Role::factory()->count(3)->create(); 
        Country::factory()->count(3)->create();
    }
}

Then run:

php artisan db:seed

Leave a Reply

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