Laravel 9 generates dummy data using factory; In this tutorial, we will show you how to generate dummy or fake data into database table using factory, faker, tinker and seeder in laravel 9 app.
Whenever you will test your Laravel app. At that time you need fake records in the database. So, you will be manually entering fake records in the database.
So, in this tutorial you will learn how to generate or create fake records into database using factory, faker, seeders and tinker command in laravel 9 app.
Laravel 9 Factory, tinker Command to Generate Dummy or Fake Data Tutorial
Follow the below simple steps and generate/create fake data into database table using factory, faker and tinker command in laravel 9:
Step 1 – Install Laravel 9 App
Install or download laravel 9 application, so open terminal or command prompt and run the following command to install fresh laravel 9 app:
composer create-project --prefer-dist laravel/laravel blog
Step 2 – Configure Database with App
Configure database in .env file with this app:
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 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 Post.php modal inside 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 factory class named PostFactory by using the following command:
php artisan make:factory PostFactory --model=Post
After that, open PostFactory.php file, which is found inside 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, ]; } }
The run the following command on command prompt to auto load dependencies:
composer dump-autoload
Step 5 – Run tinker, Factory Command
Execute the following command on command prompt to generate or create dummy data using tinker and factory command:
php artisan tinker Post::factory()->count(20)->create()
Now, visit localhost/phpmyadmin and check database posts table.