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.

AWS Q Developer image

Your AI Code Assistant

Ask anything about your entire project, code and get answers and even architecture diagrams. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Start free in your IDE

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →