Now, let's see article of laravel factory faker. it's simple example of laravel tinker factory. i would like to share with you php artisan tinker laravel. you'll learn laravel tinker tutorial.
you can also use factory tinker in laravel 6, laravel 7 and laravel 8 version.
As we 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.
However, Laravel has tinker that provide to create dummy records to your model table. so in laravel application they provide User model factory created by default. so you can see how to create records using factory bellow:
Installation:
All Laravel applications include Tinker by default. However, you may install it manually if needed using Composer:
composer require laravel/tinker
Tinker allows you to interact with your entire Laravel application on the command line, including the Eloquent ORM, jobs, events, and more. To enter the Tinker environment, run the tinker
Artisan command:
php artisan tinker
You can publish Tinker's configuration file using the vendor:publish
command:
php artisan vendor:publish --provider="Laravel\Tinker\TinkerServiceProvider"
Generate Dummy Users:
php artisan tinker
User::factory()->count(5)->create()
This by default created factory of laravel. you can also see that on 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 new factory using tinker command. here i will give you simple example of creating product factory and you will understand how it works. so let's create Product Model as like bellow:
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 bellow command:
php artisan make:factory ProductFactory --model=Product
Now they created new factory class for product and you can add as bellow code:
database\factories\ProductFactory.php
<?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,
'slug' => Str::slug($this->faker->name),
'detail' => $this->faker->text,
];
}
}
Using Faker you can be used to 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 products table by following command:
Generate Dummy Product:
php artisan tinker
Product::factory()->count(500)->create()