Generate Dummy Data using Factory Tinker In Laravel 10

  Dec 2023
  ITSolutionsGuides
  Category: Laravel
Generate Dummy Data using Factory Tinker In Laravel 10

Welcome To ITSolutionsGuides,

Lets see Generate Dummy Data using Factory Tinker In Laravel 10 using the it solutions guides tutorial. Laravel offers factory facker to generate dummy records. It was highly useful in the developing and the testing phase of the Laravel Application to create realistic data based on the columns It was mainly used during the testing purpose. During testing we need to check the designs, pagination and functions to check these we need bulk amount of records. we can't add each records manually it is a more time consuming process. To overcome these we should use Laravel factory tinker . We can also customize the data that need to be generated. Rather than creating random text we can create more realistic data by using the Factory data types. Laravel offers more data types,
name
safeEmail
phoneNumber
numberBetween()
randomElement()
address
now(()
text
color
files
barcodes
numbers

etc. Like these we can create more and more realistic data using the data types in Laravel.
Generate Laravel 10 dummy data effortlessly using Factory Tinker. Streamline testing with realistic datasets for robust application development with this it solutions guides. so lets see Generate Dummy Data using Factory Tinker In Laravel 10. The main advantage of the Factory than the Seeders are in factory we can create more realistic data in Factory, but in seeder we can only create random text only . So lets see Generate Dummy Data using Factory Tinker In Laravel 10

How to Use Factory In Seeder

Let's Start Coding

Lets create the modals using the following artisan command,
php artisan make:model Product


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'
    ];
}

Let's Start Coding

Lets create the factory using the following artisan command,
php artisan make:factory ProductFactory --model=Product

the above command will create the ProductFactory.php file, lets make some changes, In this file we need to specify the type of the faker datatypes to be generated as the values for each column. We should define faker datatype for each columns.


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,
        ];
    }
}

Let's Execute the Command

Open the Command prompt in the project root directory and then execute the following commands one by one.

php artisan tinker

Product::factory()->count(500)->create()

We hope it helps everyone. Thanks for supporting ITSolutionsGuides and keep supporting us also follow us in social media platforms.

Subscribe for NewsLetter

Be the first to know about releases and tutorial news and solutions.

We care about your data in our privacy policy.

ITSolutionsGuides

ITSolutionsGuides was started mainly to provide good and quality web solutions for all the developers. We provide tutorials to support all the developers and also we try to provide solutions to the errors we face while coding.

Contact US

ITSolutionsGuides, provide users with an easy-to-use form to reach out for support or inquiries.

whatsapp  gmail  instagram-new--v1  facebook-circled  twitter-circled  linkedin  github  pinterest 

Copyright © 2023 - 2024 All rights reserved | ITSolutionsGuides