Creating dummy data in your Laravel project is essential when building and testing applications. In this tutorial, we’ll walk through how to create a table and populate it with fake data using Laravel’s powerful Factory and Seeder features.
Step 1: Set Up Your Database
Before we begin, you need to set up your database and configure your Laravel project to connect to it. Open your .env file and update the following values based on your database credentials:
Once your database information is correctly entered, run the following command to initialize the database migrations,
This command will create all the necessary tables defined in your migrations folder, preparing the structure for the application data.
1 |
php artisan migrate |
1 |
php artisan make:migration create_posts_table --create=posts |
Step 2: Create a New Migration for the Posts Table
Now that your database is ready, let’s create a migration file for the posts table. This table will hold our sample data.
1 |
php artisan make:migration create_posts_table --create=posts |
Open the generated migration file in database/migrations and define the columns for the posts table. Here’s an example of a basic structure:
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 |
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. */ public function up(): void { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('content'); $table->timestamps(); }); } /** * Reverse the migrations. */ public function down(): void { Schema::dropIfExists('posts'); } }; |
Step 3: Create a Model for the Posts Table
After defining the table, it’s time to create a model that represents this table in your code. Run the following command to generate a model for Post:
1 |
php artisan make:model Post |
Open the Post.php file in the app/Models directory and add the HasFactory trait to allow factory usage:
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 |
class Post extends Model { use HasFactory; /** * The table associated with the model. * * @var string */ protected $table = 'posts'; /** * The attributes that are mass assignable. * * @var array<int, string> */ protected $fillable = [ 'title', 'content', ]; /** * The attributes that should be hidden for serialization. * * @var array<int, string> */ protected $hidden = []; /** * The attributes that should be cast. * * @var array<string, string> */ protected $casts = []; } |
Step 4: Create a Factory to Generate Fake Data
Laravel factories are an easy way to create dummy data. Let’s create a factory for the Post model:
1 |
php artisan make:factory PostFactory --model=Post |
Open the newly created PostFactory.php in the database/factories directory. Customize it to generate random titles and content for your posts:
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 |
<?php namespace Database\Factories; use Illuminate\Database\Eloquent\Factories\Factory; /** * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Post> */ class PostFactory extends Factory { /** * Define the model's default state. * * @return array<string, mixed> */ public function definition() { return [ 'title' => $this->faker->sentence, // Generates a random sentence for the title 'content' => $this->faker->paragraph, // Generates a random paragraph for the content // Laravel automatically handles created_at and updated_at timestamps ]; } } |
In this code, we use Laravel’s faker library to generate random titles and paragraphs, which will populate our posts table.
Step 5: Set Up a Seeder for the Posts Table
Seeders allow you to populate the database with default data. To create a seeder for the posts table, use the following command:
1 |
php artisan make:seeder PostsTableSeeder |
Edit the PostsTableSeeder.php file in database/seeders to utilize the factory we just created:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php namespace Database\Seeders; use Illuminate\Database\Seeder; class PostsTableSeeder extends Seeder { /** * Run the database seeds. */ public function run(): void { \App\Models\Post::factory()->count(10)->create(); } } |
This seeder will generate 10 posts using the PostFactory. Feel free to change the count to create more or fewer posts as needed.
Step 6: Run the Seeder
Finally, it’s time to seed the database. Run the following command to execute the PostsTableSeeder:
1 |
php artisan migrate |
1 |
php artisan db:seed --class=PostsTableSeeder |
If everything is set up correctly, you should see 10 new records in your posts table. Open your database viewer or run a query to verify the data.
You’ve successfully created a dummy posts table and populated it with fake data using Laravel factories and seeders! This setup is invaluable for testing and developing new features without requiring real data. By automating data creation, you can focus on building and improving the core functionality of your application.