0
0
Laravelframework~30 mins

Seeding data in Laravel - Mini Project: Build & Apply

Choose your learning style9 modes available
Seeding Data in Laravel
📖 Scenario: You are building a simple blog application. You want to add some initial posts to your database so you can see content when you open the app.
🎯 Goal: Create a Laravel seeder to add three specific blog posts to the posts table.
📋 What You'll Learn
Create a seeder class named PostSeeder
Define an array of posts with exact titles and content
Use the DB facade to insert posts into the posts table
Call the seeder from the DatabaseSeeder class
💡 Why This Matters
🌍 Real World
Seeding data helps developers quickly fill a database with sample or initial data for testing and development.
💼 Career
Knowing how to seed data is essential for backend developers working with Laravel to prepare databases for applications.
Progress0 / 4 steps
1
Create the posts array
Create a variable called posts that holds an array with three associative arrays. Each associative array must have keys 'title' and 'content' with these exact values:

1. Title: 'Welcome to Laravel', Content: 'This is the first post.'
2. Title: 'Seeding Data', Content: 'Learn how to seed data in Laravel.'
3. Title: 'Database Basics', Content: 'Understanding migrations and seeders.'
Laravel
Need a hint?

Use a PHP array with three elements. Each element is an associative array with keys 'title' and 'content'.

2
Create the PostSeeder class
Create a class named PostSeeder that extends Seeder. Inside it, define a run method. Inside run, add the posts array you created in Step 1.
Laravel
Need a hint?

Remember to import Seeder and define the run method inside your class.

3
Insert posts into the database
Inside the run method of PostSeeder, use DB::table('posts')->insert($posts); to insert the posts array into the posts table.
Laravel
Need a hint?

Use the DB facade to insert the array into the posts table.

4
Call PostSeeder from DatabaseSeeder
Open the DatabaseSeeder class and inside its run method, add the line $this->call(PostSeeder::class); to run your PostSeeder.
Laravel
Need a hint?

Use $this->call() inside DatabaseSeeder::run() to run your PostSeeder.