If you are working in a Laravel10 web app and you want to test your Laravel web app with dummy and fake data. So for this, you have to insert fake data in the database. So you can easily add fake and dummy data to the database using factory, faker, seeders, and tinker in Laravel apps. so that you can test your Laravel 10 web applications with fake data.
So, In this tutorial, you will learn how to generate fake data and insert dummy or fake data into MySQL database table using factory, faker, tinker, and seeder in Laravel 10 app.
How to Generate and Insert Fake Data using Faker, Factory & Seeder in Laravel 10
By using the below-given steps, you can generate and insert fake data into a database table using factory, Seedar, faker and tinker commands in Laravel 10:
- Step 1 – Create New Laravel 10 App
- Step 2 – Setup Database with App
- Step 3 – Create Model and Migration
- Step 4 – Create Factory Class
- Step 5 – Run Tinker, Factory Command to Generate Fake Data
Step 1 – Create New Laravel 10 App
First of all, start your terminal to download or install Laravel 10 new setup. Run the following commands in it to install the new Laravel 10 app on your system:
composer create-project --prefer-dist laravel/laravel blog
Step 2 – Setup Database with App
In this step, Configure your database with your apps. So, visit your app root directory and find .env file. Then configure database details as follows:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=db name DB_USERNAME=db user name DB_PASSWORD=db password
Step 3 – Create Model and Migration
Create one model and migration name Post using the following command:
php artisan make:model Post -m
After that, open the create_posts_table.php file, which is found inside /database/migrations/ directory and update the following code in it:
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreatePostsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('description'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('posts'); } }
after that, Found the Post.php modal inside the app/Models directory and update the following code into it:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Post extends Model { use HasFactory; protected $fillable = [ 'title', 'description' ]; }
Step 4 – Create Factory Class
Create a factory class named PostFactory by using the following command:
php artisan make:factory PostFactory --model=Post
After that, open the PostFactory.php file, which is found inside the database/factories/ directory. And update the following code in it:
<?php namespace Database\Factories; use App\Models\Post; use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Support\Str; class PostFactory extends Factory { /** * The name of the factory's corresponding model. * * @var string */ protected $model = Post::class; /** * Define the model's default state. * * @return array */ public function definition() { return [ 'title' => $this->faker->title, 'description' => $this->faker->text, ]; } }
Run the following command on the command prompt to auto-load dependencies:
composer dump-autoload
Step 5 – Run Tinker, Factory Command to Generate Fake Data
Execute the following command on the command prompt to generate or create dummy data using the tinker and factory commands:
php artisan tinker Post::factory()->count(20)->create()
Now, visit localhost/phpmyadmin and check the database.