In this tutorial you will learn about the Laravel 8 Factory Tinker Example Tutorial and its application with practical example.
In this Laravel 8 Factory Tinker Example Tutorial I will show you how to generate and insert dummy or fake records in your database table using laravel factory tinker in laravel 8 application.In this tutorial you will learn how to generate fake records in laravel using laravel factory tinker. You will also learn how to use laravel factory tinker. With Laravel factory tinker, you can generate bulk amount of records and insert into the database. In this tutorial you will also learn to use PHP Faker library in laravel to generate fake data. PHP Faker is a powerful PHP library that helps you to generate fake data for while developing new application.
In Laravel 8 we will be using “laravel/tinker” composer package to generate dummy records. Laravel 8 comes with “laravel/tinker” package. In database/factories folder you can add your different factory for different model.
Laravel 8 Factory Tinker Example Tutorial
In this step by step tutorial I will demonstrate you how to generate and insert dummy or fake records in your database table using laravel factory tinker. Please follow the instruction given below:
Install Laravel 8
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel lara8blog |
Make sure you have composer installed.
Setup Database Credentials
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=lara8blog DB_USERNAME=root DB_PASSWORD= |
Generate Model & Run Migration
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan make:model Product -m |
Once above command is executed there will be a migration file created inside database/migrations/ directory, just open migration file and update the function up() method as following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateProductsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('products', function (Blueprint $table) { $table->id(); $table->string('name'); $table->text('detail'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('products'); } } |
Now, go to app/Product.php and open Product model file and put the below code.
app/Models/Product.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Product extends Model { use HasFactory; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'detail' ]; } |
Create Custom Factory:
In this step you need to create a custom factory class using following artisan command:
1 |
php artisan make:factory ProductFactory --model=Product |
Now open database\factories\ProductFactory.php file and put the following code in it:
database\factories\ProductFactory.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?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, ]; } } |
Now, run following command in termina:
1 |
composer dump-autoload |
Generate Dummy Records with Factory Tinker
FInally we will be creating test data and seed them into the Product table. Use the following command:
1 2 |
php artisan tinker Product::factory()->count(500)->create() |