Laravel 10 Generate Fake Data Faker, Factory & Seeder Example

Laravel 10 Generate Fake Data Faker, Factory & Seeder Example

Simply use faker, factory, tinker, and Seeder to generate fake or dummy data and store it into the MySQL database in Laravel applications.

How to Generate Dummy or Fake Data using Faker, Factory & Seeder in Laravel 10

A step by step guide on how to generate fake or dummy data and insert it into MySQL database table using factory, faker, tinker, and seeder in Laravel 10 app.

Step 1 – Create New Laravel 10 App

First of all, start your terminal to download or install Laravel 10 new setup. Run the following commands in it to install the new Laravel 10 app on your system:

composer create-project --prefer-dist laravel/laravel blog

Step 2 – Setup Database with App

In this step, Configure your database with your apps. So, visit your app root directory and find .env file. Then configure database details as follows:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db name
DB_USERNAME=db user name
DB_PASSWORD=db password

Step 3 – Create Model and Migration

Create one model and migration name Post using the following command:

php artisan make:model Post -m

After that, open the create_posts_table.php file, which is found inside /database/migrations/ directory and update the following code in it:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->text('description');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}

After that, Open the Post.php model from the app/Models directory and update the following code into it:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasFactory;

    protected $fillable = [
        'title', 'description'
    ];
}

Step 4 – Create Factory Class

Create a factory class named PostFactory by using the following command:

php artisan make:factory PostFactory --model=Post

After that, open the PostFactory.php file, which is found inside the database/factories/ directory. And update the following code in it:

<?php

namespace Database\Factories;

use App\Models\Post;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

class PostFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Post::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'title' => $this->faker->title,
            'description' => $this->faker->text,
        ];
    }
}

Run the following command on the command prompt to auto-load dependencies:

composer dump-autoload

Step 5 – Run Tinker, Factory Command to Generate Fake Data

Execute the following command on the command prompt to generate or create dummy data using the tinker and factory commands:

php artisan tinker

Post::factory()->count(20)->create()

Now, visit localhost/phpmyadmin and check the database.

Recommended Laravel posts

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *