What is database seeding in Laravel?
Seeding your database in your Laravel application can be very useful. Database seeding is the process of programatically filling your database with data. It is commonly done during development to give your application dummy data to work with, or to insert data that you will always want in your database such as a specific set of users.
What we are going to do
In this guide we are going to seed our Laravel database with an initial set of users. We are going to create 2 users, one called Admin and one called John. These initial users won’t have any specific roles or permissions assigned to them, this will be looked at in the next guide. For now we are just going to show how to create the users using a Laravel seeder.
Generate Seeder
Generating the Laravel seeder is simple. Simply run the following artisan command:
php artisan make:seeder UserSeeder
Running the above command will create a new file called UserSeeder.php, which can now be seen in
. We will edit this file shortly.
Create Users Logic
Add the following to our newly created UserSeeder.php file:
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use App\Models\User;
class UserSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//
$users = [
[
'name' => 'Admin',
'email' => '[email protected]',
'password' => 'password'
],
[
'name' => 'John',
'email' => '[email protected]',
'password' => 'password2'
]
];
foreach($users as $user) {
User::create([
'name' => $user['name'],
'email' => $user['email'],
'password' => Hash::make($user['password']),
]);
}
}
}
A closer look
If we look at what we have just put into our new UserSeeder.php file, we can see that we have put the bulk of code into the run()
function. The code in this function is what gets run when we call this seeder. In our example we have put the logic to create new users. Our code simply loops over an array of users and creates a new user on each loop iteration.
Running Laravel seeders
When it comes to running our Laravel seeders, we have a couple of options. We can either run each individual seeder separately or run them all together at the same time. In our example we only have one seeder, the UserSeeder, but I will demonstrate how to do each method.
Running each Laravel seeder separately
To run each seeder separately, simple run the following command in the terminal. Class equals the name of our seeder.
php artisan db:seed --class=UserSeeder
At this point you have seeded your Laravel database with users! Keep reading to see how to run all your seeders with one command.
Running all Laravel seeders together with one artisan command
Add our seeder to DatabaseSeeder.php
If we want to run all the Laravel seeders that we have created with one command, we need to add each seeder to the DatabaseSeeder.php
file. Open your DatabaseSeeder.php
file and add the UserSeeder to the top of the file, also amend the run()
function:
use Database\Seeders\UserSeeder;
...
public function run()
{
$this->call([
UserSeeder::class,
]);
}
Run the artisan command
Once you have amended your DatabaseSeeder.php
file, simply run the following command to run all seeders listed in the DatabaseSeeder.php
file:
php artisan db:seed
Notice that the command to run all seeders and the command to run individual seeders is the same! The only difference between the two is that the specific class is specified in the command to run individual seeders.
I have videos too!
If you found this post helpful, please consider subscribing to the @codingoblin YouTube channel 😀