Creating dummy data in Laravel

You will need to create dummy data in Laravel for many reason

 

Maybe you haven't created the UI elements you need yet

Or you need to auto create fake data for testing

you ahve two option sfor creating fake data in Laravel

seeding and factories


To create data using factories

$ php artisan tinker

 Post::factory()->count(50)->make(); --will giv e you a preview of the fake data

 Post::factory()->count(50)->create(); will save the data to the database

 

<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use App\Models\Post;

class PostSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        Post::factory()
        ->count(50)
        //->hasPosts(1)
        ->create();
    }
}

php artisan db:seed --class=PostSeeder

 

 

https://laravel.com/docs/10.x/seeding

https://laravel.com/docs/10.x/eloquent-factories