Laravel 9 Factory Example Tutorial

10-Apr-2023

.

Admin

Laravel 9 Factory Example Tutorial

Hi Guys

In this tutorial, I will learn how to generate dummy records in your database table using the tinker factory of Laravel 9. As I know testing is a very important part of any web development project. Sometimes we may require to add hundreds of records in your user's table, OR maybe thousands of records. Also, think about if you require to check pagination. then you have to add some records for testing. So what you will do it at that moment, You will add manually thousands of records. If you add manually thousands of records then it can take more time.

Here, Laravel 9 has composer package "laravel/tinker" that we will provide you to generate dummy records by using their command. So Laravel 9 by default take "laravel/tinker" package for project. Also they provide by default one factory for user table. You can check path here :

Step 1: Download Laravel


Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app

Step 2: Add Model and Migration

Here this step, we will create one model and migration name Product. Use the below following command and create it

php artisan make:model Product -m

next,open products migration file and put the below code.

database/migrations/2020_02_03_095534_create_products_table.php

<?php

use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;

class CreateProductsTable extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::create('products', function (Blueprint $table) {

$table->id();

$table->string('name');

$table->text('detail');

$table->timestamps();

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::dropIfExists('products');

}

}

Next, go to app/Product.php and open Product model file and put the below code.

here following path of model fille.

app/Models/Product.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;

use Illuminate\Database\Eloquent\Model;

class Product extends Model

{

use HasFactory;

/**

* The attributes that are mass assignable.

*

* @var array

*/

protected $fillable = [

'name', 'detail'

];

}

Step 3: Add Factory

In this setp,I will create ProductFactory in following command As you see above command for user, But you can not do same for other model. If you want to do this then you have to add factory for that model. So i am going to give you full example of factory from scratch.

php artisan make:factory ProductFactory --model=Product

I have Product model with products table. Now we want to add dummy records for Product model. So we have to add factory like as bellow :

database/factories/ProductFactory.php

<?php

namespace Database\Factories;

use App\Models\Product;

use Illuminate\Database\Eloquent\Factories\Factory;

use Illuminate\Support\Str;

class ProductFactory extends Factory

{

/**

* The name of the factory's corresponding model.

*

* @var string

*/

protected $model = Product::class;

/**

* Define the model's default state.

*

* @return array

*/

public function definition()

{

return [

'name' => $this->faker->name,

'detail' => $this->faker->text,

];

}

}

after run command in terminal

composer dump-autoload

and

php artisan tinker

Product::factory()->count(5)->create()

It Will help you...

#Laravel 9