In today’s tutorial, we will learn about Database Seeder in Laravel 8 with Example. I will guide you on How to create Seeder, What is the command for Database Seeder and How to run Seeder in Laravel 8.
Contents
Laravel provides a simple method to seed test data into a database using seeder classes. You can perform database seeding in Laravel to add fake data into the database for testing purposes.
php artisan make:seeder UserSeeder
After you run the above command, it will create one file UserSeeder.php in the seeds folder. The seed classes are stored in the database/seeders directory.
<?php 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 doe', 'email' => 'john@gmail.com', 'mobile' => '911234567891', 'password' => Hash::make('john@123') ]); } }
The call method is used to execute additional seed classes within the DatabaseSeeder class. It allows you to break up your database seeding into multiple files so that no single seeder class becomes too large. The call method accepts an array of seeder classes that should be executed.
<?php use Illuminate\Database\Seeder; class DatabaseSeeder extends Seeder { public function run() { $this->call([ UserSeeder::class, PostSeeder::class, ]); } }
Command to run seeder
php artisan db:seed
Command for single seeder run
php artisan db:seed -–class=UserSeeder
You can also seed your database using the migrate:fresh command in combination with the –seed option. This command drops all tables, re-runs all of your migrations, and re-building your database.
php artisan migrate:fresh --seed
This way, you can create a database seeder in Laravel. If you have any doubts, connect with our experienced Laravel Developers, who will help you with your queries. Share the tutorial with your friends and stay in touch with us to learn more.
Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…
In today’s digital landscape, e-commerce has become a cornerstone for businesses looking to thrive. Among…
Flutter is a popular UI toolkit that allows developers to create beautiful, natively compiled applications…
The 403 Forbidden error is an everyday issue users may face while browsing the internet.…
The Hyva theme has significantly impacted the Magento 2 eCommerce landscape. This theme is crafted…
Magento 2 is one of the most powerful eCommerce platforms, offering extensive customization, scalability, and…
View Comments
How to add some dummy random integer type data?
You can achieve this with below code
YourModel::create([
'integer_column' => random_int(1, 100), // Example: Inserting random integer between 1 and 100
]);
you missed double dash for this command
php artisan db:seed –class=UserSeeder it should be --class=
Thank you for bringing it to our attention. We have made the necessary changes now.