Articles

Laravel Database Seeder

Laravel introduces seeders for creating test data, useful for verifying the project, with an admin user and pre datadefinished in the database.

Whenever you have an admin project that doesn't have a signup page, then what are you going to do? I mean, you have to create at least one admin user. So basically he can log in and access the entire admin panel. But you don't have the sign up page on the front end. you only have the login page. So can you create an admin user directly from the database?, if yes, you should always create a new admin user from the database directly when you create a new configuration of your project. But I will suggest you to create admin seeder so you can create admin user using laravel 8 seeder. Just fire on command to run seeder in laravel 8.

Same things, if you have pre settings configurationdefinite, you can create a settings seeder and add the pre configurationdefinished to the database table.

What is Database Seeder in Laravel

Laravel provides an easy method to seed test data into a database using seeder classes. You can seed your database in Laravel to add fake data into your database for testing purposes.

Example of Database Seeder in Laravel

First we create a seeder with the following command:

php artisan make:seeder UserSeeder

After running the command, we will have a file UserSeeder.php in the folder seeds. The classes seed are stored in the directory database/seeders.

namespace Database\Seeders;
 
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
 
class UserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        User::create([
            'name' => 'John Jackson',
            'email' => 'john@jackson.com',
            'mobile' => '123456789',
            'password' => Hash::make('john@123')
        ]);
    }
}

Now let's see how we can call other seeders. The call method is used to execute additional seed classes within the DatabaseSeeder class. It allows you to split your database seeding into multiple files so that no single seeder class gets too big. The call method accepts an array of seeder classes that need to be executed.

Innovation newsletter
Don't miss the most important news on innovation. Sign up to receive them by email.
<?php
  
use Illuminate\Database\Seeder;
   
class DatabaseSeeder extends Seeder
{
    public function run()
    {
         $this->call([
         UserSeeder::class,
         PostSeeder::class,
     ]);
    }
}

Command to run the seeder

php artisan db:seed

Command to run a seeder individually

php artisan db:seed –class=UserSeeder

You can also run the seeding of the database using the command migrate:fresh in combination with the option –seed. This command drops all tables, reruns all migrations, and rebuilds the database.

php artisan migrate:fresh --seed

Ercole Palmeri

You may also be interested in:

Innovation newsletter
Don't miss the most important news on innovation. Sign up to receive them by email.

Latest Articles

UK antitrust regulator raises BigTech alarm over GenAI

The UK CMA has issued a warning about Big Tech's behavior in the artificial intelligence market. There…

April 18 2024

Casa Green: energy revolution for a sustainable future in Italy

The "Green Houses" Decree, formulated by the European Union to enhance the energy efficiency of buildings, has concluded its legislative process with…

April 18 2024

Ecommerce in Italy at +27% according to the new Report by Casaleggio Associati

Casaleggio Associati's annual report on Ecommerce in Italy presented. Report entitled “AI-Commerce: the frontiers of Ecommerce with Artificial Intelligence”.…

April 17 2024

Brilliant Idea: Bandalux presents Airpure®, the curtain that purifies the air

Result of constant technological innovation and commitment to the environment and people's well-being. Bandalux presents Airpure®, a tent…

April 12 2024