Hi Guys In this tutorial, I will learn how to generate dummy records in your database table using tinker factory of Laravel 8. As I know testing is very important part of any web development project. Sometime we may require to add hundreds records in your users 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 that moment, You will add manually thousands of records ? What you do ?. If you add manually thousands of records then it can be take more time. Here,Laravel 8 have composer package "laravel/tinker" that we will provide you to generate dummy records by using their command. So Laravel 8 by default take "laravel/tinker" package for project. Also they provide by default one factory for user table. You can check path here : database/factories/. In this folder you can add your different factory for different model. Generate Dummy Products:
Here, I will give you full example for simply create dummy data using tinker factory using laravel 8 as bellow. Step 1 : Install Laravel 8 Applicationwe are going from scratch, So we require to get fresh Laravel application using bellow command, So open your terminal OR command prompt and run bellow command:
php artisan tinker
Product::factory()->count(5)->create()
Step 2: Create Model and MigrationHere this step, we will create one model and migration name Product. Use the below following command and create it
composer create-project --prefer-dist laravel/laravel blog
next,open products migration file and put the below code. Path: /database/migrations/2020_02_03_095534_create_products_table.php
php artisan make:model Product -m
Next, go to app/Product.php and open Product model file and put the below code. here following path of model fille Path:/app/Models/Product.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');
}
}
Step 3: Create FactoryIn 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
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'
];
}
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 artisan make:factory ProductFactory --model=Product
after run command in terminal
<?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,
];
}
}
and
composer dump-autoload
It Will help you...
php artisan tinker
Product::factory()->count(5)->create()
Comments