It is important to test application with adequate database. When we first create application, we don't have data. We have to create lots of dummy data with automation. Laravel 8 comes a composer package laravel/tinker that we will help you to generate dummy records by using their command.
In this tutorial, I will show you how to generate dummy records in your database table using tinker factory of Laravel 8. We will create application step by step from the scratch.
- Step: 1 Create Laravel 8 application
- Step: 2 Database configuration in .env file
- Step: 3 Create model and migration file
- Step: 4 Create factory
Follow the below steps to create dummy data using tinker factory in laravel 8.
Step 1 : Create Laravel 8 application
We need to get fresh Laravel application using below command, So open your Terminal or Command Prompt and run below command:
composer create-project --prefer-dist laravel/laravel products
Step 2: Database configuration in .env file
In the second step, we need to configure database in .env file at the root of application. Change it according to your mysql credentials.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=products
DB_USERNAME=root
DB_PASSWORD=secret
Step 3: Create model and migration file
In the third step, we will create model and migration file. For that, use the below command and create it.
php artisan make:model Product -m
This will create migration file at database/migrations
folder. Open the migration file and add the below lines in it.
<?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('description');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('products');
}
}
We also need to add $fillable property into Product model. So open Product model at app/Models folder and add $fillable property with table fields.
<?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',
'description'
];
}
Now run the following migrate command to create products table into database.
php artisan migrate
Step 4: Create factory
In this step, we will create factory for Product model. To create factory, run the below command into Terminal:
php artisan make:factory ProductFactory --model=Product
This will create factory class at database/factories folder. Now we need to add products tatble fields with datatype we want to add into table. So open factory file and change it as below.
<?php
namespace Database\Factories;
use App\Models\Product;
use Illuminate\Database\Eloquent\Factories\Factory;
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,
'description' => $this->faker->text,
];
}
}
You might need to dump-autoload composer using below composer command into Terminal.
composer dump-autoload
Now, we need to run the factory using Laravel tinker. So from Terminal, first start tinker using command:
php artisan tinker
And create as much dummy data as you want. Just pass number into count() method.
Product::factory()->count(100)->create()
This will create 100 products dummy records.
This way, you can create dummy data using tinker and factory into Laravel 8. Thanks for giving time to reading the article.