Code your dreams into reality.
Every line of code is a step towards a better future.
Embrace the bugs, they make you a better debugger.

Laravel 11 Generate Dummy Data using Factory Tinker Tutorial

Last Updated on April 9, 2024 by

In Laravel 11, you need to dummy or fake data for testing purposes. To create this you have to create a factory class and add definitions to it, then use TINKER commands with the help of which can you generates fake or dummy data and inserts it in the database.

Getting started with Factory and Tinker to create dummy data and insert into database in Laravel 11:

Step 1: Create a Model Factory

Create a model factory for the Note model using Artisan command:

php artisan make:factory NoteFactory --model=Note

Step 2: Define Factory Attributes

Open the generated NoteFactory.php file from database/factories Folder. Define the attributes for the Note model like the following:

<?php

namespace Database\Factories;
use App\Models\Note;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Note>
 */
class NoteFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
      
    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition(): array
    {
        return [
            'title' => $this->faker->name,
            'description' => $this->faker->text,
            //attributes
        ];
    }


}

In Laravel factories, you can use various methods provided by the Faker library to generate dummy data for different types of attributes. Here are some:

Data Type Faker Method Description
String $faker->word Generates a single random word.
$faker->sentence Generates a random sentence.
$faker->text($maxNbChars = 200) Generates a paragraph of random text.
Numeric $faker->randomNumber($nbDigits = 5) Generates a random number.
$faker->randomFloat($nbMaxDecimals = NULL, $min = 0, $max = NULL) Generates a random floating-point number.
$faker->boolean($chanceOfGettingTrue = 50) Generates a boolean value.
Date/Time $faker->date($format = ‘Y-m-d’, $max = ‘now’) Generates a random date.
$faker->time($format = ‘H:i:s’, $max = ‘now’) Generates a random time.
$faker->dateTime($max = ‘now’, $timezone = null) Generates a random datetime.
Other $faker->email() Generates a random email address.
$faker->url() Generates a random URL.
$faker->phoneNumber() Generates a random phone number.
$faker->ipv4() Generates a random IPv4 address.

Step 3: Generate Dummy Data

Open terminal or cmd and run Tinker Factory command to generate dummy data for the notes table and insert into it in database:

php artisan tinker

On the run the following command on Tinker shell:

use App\Models\Note;

$notes = Note::factory()->count(10)->create();

Step 4: Exit Tinker

Once you are done creating and inserting dummy data into the database, exit the Tkinter shell:

exit

Laravel 11 has Tkinter and Factory built in commands that help you create fake or dummy data and insert it into the database as taught to you in the guide.

For reference guides:

Leave a Comment