Laravel 9 Database Seeder Example
Teams applying “Laravel 9 Database Seeder Example” in a production project can use cost poor quality to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.
Hello Friends,
In this blog, I will show you how to create database seeder in laravel 9 application. I will let you know example of laravel 9 seeder example. As an example, let's modify the default DatabaseSeeder class and add a database insert statement to the run method.
Laravel provides a tool to add sample or dummy data to our databases automatically. that is call it database seeding.
Whenever you have admin project that don't have register page then what you will do?, i mean you need to create at least one admin user. So basically he can login and access whole admin panel. But you don't have register page on front end. you only have login page. So you can create one admin from database directly?, if yes then you have to always create new admin user from directly database when you create new setup of your project. But i will suggest you to create admin seeder so you can create admin user using laravel 9 seeder. You just fire on command to run seeder in laravel 9.
In this tutorial, i will show you how to create database seeder in laravel 9 and what is command to create seeder and how to run that seeder in laravel 9. so you have to just follow few step get how it's done.
Laravel gives command to create seeder in laravel. so you can run following command to make seeder in laravel application.
Download Laravel
Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.
composer create-project laravel/laravel example-app
Create Seeder Command:
php artisan make:seeder AdminSeeder
after run above command, it will create one file AdminSeeder.php on seeds folder. All seed classes are stored in the database/seeds directory.
Then you can write code of create admin user using model in laravel 8.
database/seeds/AdminSeeder.php
<?php
use Illuminate\Database\Seeder;
use App\Models\User;
class AdminSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
User::create([
'name' => 'Admin',
'email' => '[email protected]',
'password' => bcrypt('123456'),
]);
}
}
As you can see above code, i simply write creating new user in my users table now. so you can add your code to write admin user creating. Now how you can run this seeder manually.
There is a two way to run this seeder. i will give you both way to run seeder in laravel 8.
Way 1 : Single Seeder Run
You can run only single perticuler then using bellow command Here i will run AdminSeeder:
php artisan db:seed --class=AdminSeeder
Way 2 : Run all Seeders
In this way, you have to declare your seeder in DatabaseSeeder class file. then you have to run single command to run all listed seeder class.
So can list as bellow:
database/seeds/DatabaseSeeder.php
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
$this->call(AdminSeeder::class);
}
}
Now you need to run following command for run all listed seeder:
php artisan db:seed --class=DatabaseSeeder
Now i think you will understand how seeding is work and we have to use in our laravel app.
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial