DEV Community

Cover image for ๐Ÿš€ Setting Up and Seeding a MySQL Database in Laravel (Beginner Guide)
Rohit Dhiman
Rohit Dhiman

Posted on

๐Ÿš€ Setting Up and Seeding a MySQL Database in Laravel (Beginner Guide)

๐Ÿš€ 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=
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

๐ŸŒฑ 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
Enter fullscreen mode Exit fullscreen mode

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 App\Models\User;

public function run()
{
    User::factory(10)->create();
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” 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),
    ];
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ฆ Step 5: Register and Run the Seeder

Inside database/seeders/DatabaseSeeder.php, register your custom seeder:

$this->call(UserSeeder::class);
Enter fullscreen mode Exit fullscreen mode

Then, run the seeder using:

php artisan db:seed
Enter fullscreen mode Exit fullscreen mode

Or for a full reset and reseed:

php artisan migrate:fresh --seed
Enter fullscreen mode Exit fullscreen mode

โœ… 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

Top comments (0)