In this tutorial you will learn about the Laravel 9 Generate Dummy Data Using Factory Tutorial and its application with practical example.
In this Laravel 9 Generate Dummy Data Using Factory Tutorial, I will show you how to generate dummy data using factory, faker, tinker and seeder in laravel 9 application. In this tutorial you will learn to generate fake or dummy data in a database table using factory in laravel 9. While working with any web application using laravel we come situations where we need some dummy or test data to available in database. Laravel factory makes this job much easier using laravel factory we can generate dummy data into the database table.
Laravel 9 Generate Dummy Data Using Factory Tutorial
In this tutorial, you will learn how to generate dummy data into the database tables using factory in laravel 9 application. Please follow the instruction give below:
- Install Laravel 9
- Setup Database Credential
- Create a Model & Migration File
- Create New Factory File
- Generate Fake Data Using Tinker
Install Laravel 9
First of all we need to create a fresh laravel project, download and install Laravel 9 using the below command
1 |
composer create-project --prefer-dist laravel/laravel lara9blog |
Configure Database
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
.env
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=lara9blog DB_USERNAME=root DB_PASSWORD= |
Create Model & Migration
Now, we have to define table schema for posts table. Open terminal and let’s run the following command to generate a migration along with model file to create posts table in our database.
1 |
php artisan make:model Post -m |
Once this command is executed you will find a migration file created under “database/migrations”. lets open migration file and put following code in it –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?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'); } } |
Next, go to app/Models/Post.php file and add the $fillable property in the Post model as following.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?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' ]; } |
Now, run following command to migrate database schema.
1 |
php artisan migrate |
After, the migration executed successfully the posts table will be created in database.
Create New Factory File
In this step, we will create a factory file. Run the following command to create factory class named PostFactory by using the following command:
1 |
php artisan make:factory PostFactory --model=Post |
Go to app/database/factories and open PostFactory.php and put the below code into your file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?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 to auto load dependencies.
Generate Fake Data Using Tinker
Please use the following command to generate fake data using tinker:
1 |
php artisan tinker |
Now, run following command to generate 100 rows of random Notes.
1 |
Post::factory()->count(100)->create() |