Laravel 10 Generate Test or Dummy Data using Factory Tinker
This article goes in detailed on laravel 10 factory tinker example. I would like to show you laravel 10 factory seeder. I would like to show you laravel 10 factories. This tutorial will give you a simple example of laravel 10 factory tutorial.
As we know testing is a very important part of any web development project. Sometimes we may require to add hundreds of 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 at that moment, You will add manually 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 to create dummy records to your model table. so in the laravel application, they provide a User model factory created by default. so you can see how to create records using the factory below:
Generate Dummy Users:
php artisan tinker
This by default created a factory of laravel. you can also see that on 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 like below:
class Product extends Model
protected $fillable = [
'name', 'slug', 'detail'
now let's create our custom factory using bellow command:
php artisan make:factory ProductFactory --model=Product
Now they created new factory class for product and you can add as bellow code:
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Product>
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
'name' => $this->faker->name,
'slug' => Str::slug($this->faker->name),
'detail' => $this->faker->text,
Using Faker you can be used to generate the following data types:
Person i.e. titles, names, gender etc.
Internet i.e. domains, URLs, emails etc.
Payments i.e. MasterCard
As you see above you can simply use data types. Now we are going to create 500 records of products table by following command:
Generate Dummy Product:
php artisan tinker
So i hope you created your 500 dummy records on products table.
I hope it can help you...
We are Recommending you
- How to Get Last Executed Query in Laravel 10?
- Laravel 10 Select2 Ajax Autocomplete Search Example
- Laravel 10 Get Client IP Address Example
- Laravel 10 Cron Job Task Scheduling Tutorial
- Laravel 10 Clear Cache of Route, View, Config, Event Commands
- Laravel 10 Guzzle Http Request Example
- Laravel 10 Change Date Format Examples
- Laravel 10 Yajra Datatables Tutorial Example
- Laravel 10 Markdown | Laravel 10 Send Email using Markdown Mailables
- Laravel 10 REST API Authentication using Sanctum Tutorial
- Laravel 10 Ajax Image Upload Example
- How to Create Migration in Laravel 10?
- Laravel 10 Import Export Excel and CSV File Tutorial