Laravel Create Dummy Data Using Factory Tinker
Hello artisans, as we know sometimes we may need to create dummy records for our testing purpose. For that purpose, Laravel has a Tinker Support which provide us ability to create dummy records. So, in this article we are going to show how Laravel tinker works. So, no more talking and get to dive into the topic.
Note: Tested on Laravel 8.6.2
Table of Contents
Install Laravel and Basic Config
Each Laravel project needs this thing. For that you can see the very beautiful article on this topic from here: Install Laravel and Basic Configurations.
Create Factory Class
Here I will show you the example of creating Student Factory and you will understand its workflow. So, let’s create a Student model first;
php artisan make:model Student -m
App\Models\Student.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
use HasFactory;
protected $fillable = [
'name', 'roll', 'class'
];
}
configure your migration file
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateStudentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('students', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->integer('roll');
$table->integer('class');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('students');
}
}
migrate your file using the following command
php artisan migrate
now let’s create our factory class using the following command
php artisan make:factory StudentFactory --model=Student
database\factories\StudentFactory.php
<?php
namespace Database\Factories;
use App\Models\Student;
use Illuminate\Database\Eloquent\Factories\Factory;
class StudentFactory extends Factory
{
protected $model = Student::class;
public function definition()
{
return [
'name' => $this->faker->name,
'roll' => $this->faker->randomNumber(),
'class' => $this->faker->numberBetween(1,9),
];
}
}
Faker Data Types
Using Faker we can generate the following data types:
- Numbers
- Lorem text
- Person i.e. titles, names, gender etc.
- Addresses
- Phone numbers
- Companies
- Text
- DateTime
- Internet i.e. domains, URLs, emails etc.
- User Agants
- Colour
- Images
- Uuid
- Barcodes
- Files
You can get more data types from this GitHub repository.
Generate Dummy Records
Here we are going to create 500 students of different classes with random roll numbers using the following command
php artisan tinker
App\Models\Student::factory()->count(500)->create()
Output:
and you will see the output similar to the following
=> Illuminate\Database\Eloquent\Collection {#5464
all: [
App\Models\Student {#5465
name: "Joel Reichel",
roll: 5363071,
class: 1,
updated_at: "2021-10-05 18:23:13",
created_at: "2021-10-05 18:23:13",
id: 1,
},
App\Models\Student {#5466
name: "Mrs. Celine Haag",
roll: 8891328,
class: 4,
updated_at: "2021-10-05 18:23:13",
created_at: "2021-10-05 18:23:13",
id: 2,
},
App\Models\Student {#5463
name: "Prof. Isaiah Bailey DVM",
roll: 291994658,
class: 4,
updated_at: "2021-10-05 18:23:13",
created_at: "2021-10-05 18:23:13",
id: 3,
},
That’s all. You can download this project from GitHub. Thanks for reading.