Laravel 11 Generate Fake Data using Factory Tinker Example
In this article, I will show you how to generate fake data using factory tinker in laravel 11 application.
As we know, testing is a very important part of any web development project. Sometimes we may require to add hundreds of records to our 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 will you do at that moment? Will you manually add thousands of records? What do you do? If you add manually thousands of records, then it can take more time.
However, Laravel has a tinker that provides the ability to create dummy records for your model table. So in the Laravel application, they provide a User model factory created by default. You can see how to create records using the factory below:
Generate Dummy Users:
php artisan tinker
User::factory()->count(5)->create()
This by default created a factory of Laravel. You can also see that at the following URL: `database/factories/UserFactory.php`.
Create Custom Factory:
But when you need to create dummy records for your products, items, or admin table, then you have to create a new factory using the tinker command. Here, I will give you a simple example of creating a product factory, and you will understand how it works. So let's create a Product Model as shown below:
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;
protected $fillable = [
'name', 'slug', 'detail'
];
}
Now let's create our custom factory using the below command:
php artisan make:factory ProductFactory --model=Product
Now they created a new Factory class for products, and you can add the code below:
database\factories\ProductFactory.php
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use App\Models\Product;
/**
* @extends \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(): array
{
return [
'name' => $this->faker->name,
'slug' => Str::slug($this->faker->name),
'detail' => $this->faker->text,
];
}
}
Using Faker, you 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 Agents
Payments i.e. MasterCard
Colour
Files
Images
uuid
Barcodes
As you see above, you can simply use data types. Now we are going to create 500 records of the products table by following command:
Generate Dummy Product:
php artisan tinker
Product::factory()->count(500)->create()
So I hope you've created your 500 dummy records in the products table.
Output:
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel 11 Flash Message Example Tutorial
- Laravel 11 Get Client IP Address Example
- Laravel 11 Select2 Ajax Autocomplete Search Example
- Laravel 11 Ajax CRUD Operation Tutorial Example
- Laravel 11 Cron Job Task Scheduling Tutorial
- Laravel 11 Send Email using Queue Example
- Laravel 11 REST API Authentication using Sanctum Tutorial
- Laravel 11 Markdown | Laravel 11 Send Email using Markdown Mailables
- Laravel 11 Create Custom Helper Functions Example
- How to Send Email using Gmail in Laravel 11?
- Laravel 11 Import Export Excel and CSV File Tutorial
- Laravel 11 Generate PDF File using DomPDF Example
- Laravel 11 CRUD Application Example Tutorial