Menu

Laravel Generate Demo Data Using Faker

Hello Guys,
Today we are going to learn about the faker package. As the name indicates, the faker creates fake data for the project. Suppose we decide to insert manual data in the database for testing. then It will consume time. To save this time we can use Faker to insert data in the database. With faker, we can create multiple records within some time.
So without wasting, Let's start the tutorial. This tutorial is divided into multiple small steps.
Let's start with the first step.

Installing Laravel

Our first step is to install the laravel. To install laravel open your terminal or command prompt and run the following command.
 

composer create-project laravel/laravel faker

This command will be a directory named faker with laravel. As we know we will create data in the database. So, Our next step is set up our project with the database.

Configure database

To configure the database. Open the .env file in the root of the laravel project. Change the database details according to your configurations. 
 

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=faker
DB_USERNAME=root
DB_PASSWORD=

By changing the .env file. Our project is connected to the database. 
 

Create Model and migration file.

Our next step is to create a model and migration file. For this Let's run the following command in the terminal or command prompt.
 

php artisan make:model Post -a

This command will create a Model with the name Post. -a attribute tell the command to create the following related files to the model. It will create Post Model, Controller, Request, Migration, Seeder, Faker and etc. 
You can also create these files by running the individual commands for each file.
Let's configure our migration file to create a table in the database.
 

Configure migration file


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

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

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

Run migrate command to create the table in the database.

php artisan migrate

After Generating the table in the database. Let's configure the Faker file. Open the following file in your code editor. I am using sublime for that, you can use any.
database/factories/PostFactory.php . Now we need to add our table fields in the return array.

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Post>
 */
class PostFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'title' => fake()->text,
            'short_description' => fake()->text,
            'body' => fake()->paragraph,
        ];
    }
}

After setting the factory file. Open the seeder file database/seeders/PostSeeder.php
Edit files as below.
 


namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class PostSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        \App\Models\Post::factory(10)->create();
    }
}

Now, We are all set to generate fake data in the database. You can run the following code to generate the data.
 

php artisan db:seed --class=PostSeeder

After running this command, It will generate 10 records in the post table.

You can also create fake records using faker and tinker. You can run the following command to generate data with the tinker. Open the terminal runs the tinker command and also run the following command in the tinker.

App\models\Post::factory(20)->create()

This command will generate 20 records in the database. 
By using these 2 methods you can create a number of records in our database. By using this data we can create our staging product. These data are also very useful in testing any of project.
I hope this tutorial is helpful for you.
Don't forget to share this post with your team and friends.

Thank You.

 
 

713
Search

Ads