In this article, we will see the laravel 8 database seeder example. As we all know laravel framework provides many functionalities to the user to reduce the developer's time for developing a website. Here, we will see how to create database seeder in laravel 8.
Many times you have a requirement to add some default records or entries to log in or add form details etc. there is no problem adding manual data in the database one or two times but it is a very challenging task to add data manually each and every time. So, if you want to avoid this boring task you need to create database seeders in laravel, laravel provides an inbuilt command to create database seeders in laravel 8.
So, let's see how to create seeder in laravel 8 and laravel 8 seeder multiple records.
So, run the following command in your terminal to create a seeder in the laravel application.
php artisan make:seeder UserSeeder
Now, it will create one file UserSeeder.php in the seeds folder. All seed classes are stored in the database/seeds directory.
Add your data as per your requirements like below in this path database/seeds/UserSeeder.php
<?php
use Illuminate\Database\Seeder;
use App\Models\User;
class UserSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
User::create([
'name' => 'test',
'email' => '[email protected]',
'password' => bcrypt('123demo'),
]);
}
}
Now, run the below command in your terminal.
php artisan db:seed --class=UserSeeder
You can declare your seeder in the DatabaseSeeder class file. then you have to run a single command to run all listed seeder classes.
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
$this->call(UserSeeder::class);
}
}
and run the below command in your terminal.
php artisan db:seed
You might also like:
In this tutorial we will see how to insert data into MySQL table using Node.js. In previous node .js article I will give...
Sep-29-2021
In the dynamic realm of modern web development, ensuring that APIs are well-documented, easy to comprehend, and seamless...
Aug-18-2023
In this article, we will show you laravel 9 user role and permission with an example. here we will see how to set u...
Mar-02-2022
Today we will see how to download file on the ftp server using php. Many time we have requirment to retrieve file from t...
May-21-2021