In this section we will see how to create dummy fake data in laravel 9. There is 2 way we can run or create dummy data using factory or tinker command.
Note: Before we will start you should have php 8.x version in your system. Because laravel 9 support php 8 & above php version.
Step 1: Set Up Laravel Project
Step 2: Set Up Database Details in ENV
Step 3: Create Modal Migration and Factory
Step 4: Run Migration & Factory
Step 1: Set Up Laravel Project
Installing a fresh new laravel 9 application, so head over to the terminal, type the command, and create a new laravel app.
composer create-project laravel/laravel laravel-faker
Step 2: Set Up Database Details in ENV
Now, you have to connect the laravel app to the database, hence open the .env configuration file and add the database credentials as suggested below.
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=database_user_name
DB_PASSWORD=database_password
Step 3: Create Modal Migration and Factory
Create Blog Modal with migration
php artisan make:model Blog -m
Create Blog Factory
php artisan make:factory BlogFactory --model=Blog
Shorthand
You can simply create Modal with migration & Factory in single command.
php artisan make:model Blog -mf
2022_02_16_075505_create_blogs_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('blogs', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('image');
$table->text('description');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('blogs');
}
};
app/database/factories/BlogFactory.php
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Blog>
*/
class BlogFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'title' => $this->faker->sentence(),
'image' => $this->faker->imageUrl(640,480),
'description' => $this->faker->text(),
];
}
}
app/database/seeders/DatabaseSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
\App\Models\User::factory(10)->create();
\App\Models\Blog::factory(10)->create();
}
}
Step 4: Run Migration & Factory
Run command in your terminal.
php artisan db:seed
or
php artisan migrate:fresh --seed
Create Fake Data using Tinker
You can also create fake dummy data using tinker. just follow above 1 to 3 step. then start tinker and run below commad.
php artisan ti
Psy Shell v0.11.1 (PHP 8.0.15 — cli) by Justin Hileman >>> App\Models\Blog::factory(10)->create();
Read Also
Laravel 9 Add Simple Sidebar with Tailwind CSS Example
How to Use Carousel Slider in Laravel 9 Example
Laravel 9 Posts with Tags Many to Many Relationships Example
Laravel 9 Insert Category in Posts CRUD Example
How to Use Ckeditor 5 in Laravel 9 Vite with Tailwind CSS
Laravel 9 Simple Image Upload in Ckeditor 5 Example
Laravel 9 Flash Message Timeout and Hide Message Example
Install & Setup Markdown Editor in Laravel 9
Nuxt 3 Data Fetching Using Laravel 9 Api Example
Laravel 9 Image Upload with Preview using Tailwind CSS & Alpine JS
Laravel 9 with Tailwind CSS Form Validation Example
Laravel 9 Backend Api Connect with Vue 3 Using Axios Example
Laravel 9 Authentication with Next js Example
Laravel 9 Sanctum Authentication with Nuxt JS Example
Laravel 9 Simple Search with Pagination Example
Laravel 9 Install Setup TALL(Tailwind, Alpinejs, Livewire) Admin Panel
How to Fix and Clean Code Style in laravel 9
Laravel 9 Image File Upload Example
3 Way to Create Slug in Laravel 9 without Package
How to Add Dark Mode in Laravel 9 with Tailwind CSS