DEV Community

Cover image for Seeding in Laravel
Sunday Moses
Sunday Moses

Posted on

Seeding in Laravel

What is seeding

This is the adding dummy data to the database using the command line.

How to generate seeder file

By using the command below, you create a seeder file called MembersSeeder.
php artisan make:seeder MembersSeeder

After creating the seeder file, then ensure to register the Str method and Facades mehtod.

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

Put the above code in the seeder file.

Data seeding

Add the following lines of code to the seeder file:
class MembersSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
//
DB::table("members")->insert([
'name' =>Str::random(10),
'email' =>Str::random(10).'@gmail.com',
'address' =>Str::random(10),
]);
}
}

Always ensure that the schema matches the seeder file definition.

And then run the following command:

php artisan db:seed --class=MembersSeeder

To add another seed into the table, just repeat the above command.

Remember you must have the migrations (table and schematic definition for the tables before you can be able to do seeding)

Check my previous post on how to make migrations.

πŸ’‘ One last tip before you go

Tired of spending so much on your side projects? πŸ€”

We have created a membership program that helps cap your costs so you can build and experiment for less. And we currently have early-bird pricing which makes it an even better value! πŸ₯

Check out DEV++

Top comments (0)

Image of Bright Data

Maintain Seamless Data Collection – No more rotating IPs or server bans.

Avoid detection with our dynamic IP solutions. Perfect for continuous data scraping without interruptions.

Avoid Detection

πŸ‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay