DEV Community

DaleLanto
DaleLanto

Posted on

Laravel 8 - Factory and Seeders - Generating Thousands of Dummy Data Instantly

Setup : Create your laravel app and connect to database
composer create-project laravel/laravel laravel_api

open .env and configure database

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_api
DB_USERNAME=root
DB_PASSWORD=
Enter fullscreen mode Exit fullscreen mode

Create the database schema laravel_api in your database
Generate the tables using:
php artisan migrate

Now let's open the app ang navigate to the folders, you will see in app/database/factories that laravel have already created a sample factory for us using table users

Let's make some minor edit so that we can use any of the dummy users in the future, let's change the password to this:

 public function definition()
    {
        return [
            'name' => $this->faker->name(),
            'email' => $this->faker->unique()->safeEmail(),
            'email_verified_at' => now(),
            'password' => bcrypt('password'), // password
            'remember_token' => Str::random(10),
        ];
    }
Enter fullscreen mode Exit fullscreen mode

Now let's use it to create 10 sample dummy data at first!

Go to terminal and use php artisan tinker

Type and enter the following:

\App\Models\User::factory()->count(10)->create();
Enter fullscreen mode Exit fullscreen mode

Hurray! Just like that we instantaneously created 10 dummy data that we can use in our app, check the database and you will there are 10 rows added in there!

Now to generate thousands just replace 10 with any amount you like just like this:

\App\Models\User::factory()->count(10000)->create();
Enter fullscreen mode Exit fullscreen mode

You now have 10000 users!
Now that we got a taste of it let's create our own!

Let's create our own model, migration, factory and seeder first
php artisan make:model Todo -mfs

Navigate to app/database/migrations and open "...create_todos_table"

Let's add columns string, text, completed and created_by which is a foreign key of user in the migration:

Schema::create('todos', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->text('body');
            $table->boolean('completed')->default(false);
            $table->unsignedBigInteger('created_by');

            $table->foreign('created_by')->references('id')->on('users')->onDelete('cascade');
            $table->timestamps();
        });
Enter fullscreen mode Exit fullscreen mode

Now let's run php artisan migrate to generate the table in our database

Let's then open the TodoFactory in app/database/factories and paste this:

public function definition()
    {
        return [
            'title' => $this->faker->sentence,
            'body' => $this->faker->paragraph,
            'completed' => rand(0,1),
            'created_by' => rand(1,10),
        ];
    }
Enter fullscreen mode Exit fullscreen mode

This will be the rules of the contents of our Todo table

Go to app/database/factories/seeders and open TodoSeeder and paste this inside to generate 3000 todo data:

namespace Database\Seeders;

use App\Models\Todo;
use Illuminate\Database\Seeder;

class TodoSeeder extends Seeder
{
    public function run()
    {
        Todo::factory(3000)->create();
    }
}
Enter fullscreen mode Exit fullscreen mode

To run the seeder let's paste it first inside the DatabaseSeeder.php in the same folder

$this->call(TodoSeeder::class);
Enter fullscreen mode Exit fullscreen mode

Go to terminal and run that specific seeder:
php artisan db:seed --class=TodoSeeder

Hurray! Now you have added 3000 rows of Todos in your database table instantly!

To run all the Database seeders inside it use this:
php artisan db:seed

This is a very useful function for setting up your laravel app specially when you are working with a team!

Top comments (0)