🚀 Setting Up and Seeding a MySQL Database in Laravel (Beginner Guide)
If you’re starting your journey with Laravel, one of the first things you’ll need to do is connect your application to a MySQL database and fill it with some sample data.
In this article, I’ll walk you through how to set up a MySQL database in Laravel and seed it with fake test data using Laravel’s built-in tools.
🛠 Step 1: Configure Your .env
File
Open your Laravel project’s .env
file and update your database credentials.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_demo
DB_USERNAME=root
DB_PASSWORD=
Make sure your MySQL server is running and the database (laravel_demo
) exists. If not, create it via phpMyAdmin, MySQL CLI, or any DB tool like TablePlus or DBeaver.
🧱 Step 2: Run Migrations
Laravel migrations allow you to define your database structure in code.
php artisan migrate
This command creates default tables such as users
, password_resets
, failed_jobs
, etc.
If you ever need to reset your database and start fresh:
php artisan migrate:fresh
🌱 Step 3: Create and Run a Seeder
🔹 What is a Seeder?
A seeder is a class that helps you populate your database with fake or static test data — perfect for development and testing.
🔹 Create a Seeder:
php artisan make:seeder UserSeeder
This generates a file in database/seeders/UserSeeder.php
.
🔹 Add Code to Seeder:
Open the UserSeeder.php
file and use a factory to create fake users.
use AppModelsUser;
public function run()
{
User::factory(10)->create();
}
🔁 This will generate 10 fake users using Laravel’s
UserFactory
.
🧪 Step 4: Use the Factory
Make sure your factory is defined properly in database/factories/UserFactory.php
:
public function definition()
{
return [
'name' => $this->faker->name(),
'email' => $this->faker->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => bcrypt('password'),
'remember_token' => Str::random(10),
];
}
📦 Step 5: Register and Run the Seeder
Inside database/seeders/DatabaseSeeder.php
, register your custom seeder:
$this->call(UserSeeder::class);
Then, run the seeder using:
php artisan db:seed
Or for a full reset and reseed:
php artisan migrate:fresh --seed
✅ Why Should You Seed?
Seeding is useful because it:
- Creates consistent fake data for development/testing
- Speeds up setup for new environments
- Avoids the hassle of manual database entry
- Works great for testing UI with realistic content
🧠 Pro Tip:
You can create seeders and factories for any model — not just User
. Combine factories with relationships for advanced data seeding!
🔚 Final Words
Whether you’re building a blog, an admin dashboard, or an eCommerce app — seeding your Laravel database is a fundamental step that makes development smoother.
🧩 GitHub Example
Want a working demo?
👉 Laravel Seeders Example on GitHub
🙌 Let’s Connect!
If you enjoyed this article, share your thoughts or ask questions below.
Follow me on LinkedIn / Substack / GitHub for more beginner-friendly Laravel tutorials.
💬 Have questions or want more beginner Laravel tutorials? Drop a comment or reach out — happy to help!
#Laravel #MySQL #PHP #WebDevelopment #CodingForBeginners #LaravelTips #DevCommunity