Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.

Get Started Now!

Database configuration & migration in Laravel

In Laravel, database configuration and migration are fundamental tasks for setting up and managing your application’s database. Here’s how you can handle both:

1. Database Configuration

Before you can run migrations, you need to configure your database in Laravel. This is done in the .env file located in your project’s root directory. Here’s a typical configuration for a MySQL database:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_username
DB_PASSWORD=your_database_password

Make sure to replace your_database_name, your_database_username, and your_database_password with your actual database details. Laravel supports other databases like PostgreSQL, SQLite, and SQL Server as well.

2. Migration Basics

Migrations are like version control for your database, allowing you to modify and share the application’s database schema. You can generate a new migration file using the Artisan command line tool provided by Laravel.

Creating a Migration

To create a migration, use the make:migration command:

php artisan make:migration create_users_table --create=users

This command creates a new migration for a users table. The migration file will be located in the database/migrations directory.

Migration Structure

A migration file contains two methods: up() and down(). up() is used to add new tables, columns, or indexes to your database, while down() should reverse the operations performed by the up() method.

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

public function down()
{
    Schema::dropIfExists('users');
}

3. Running Migrations

To apply the migrations, use:

php artisan migrate

This command will execute the up() method in your migrations that haven’t been run yet.

4. Rolling Back Migrations

If you need to undo a migration, you can use:

php artisan migrate:rollback

This command will call the down() method for the last batch of migrations that were applied.

5. Database Seeding

After migrating, you might want to populate your database with initial data. This can be done using seeders.

Creating a Seeder

Generate a seeder via Artisan:

php artisan make:seeder UsersTableSeeder

Then, you can define the data insertion within the run() method of your seeder:

public function run()
{
    DB::table('users')->insert([
        'name' => 'Example User',
        'email' => 'user@example.com',
        'password' => Hash::make('password'),
    ]);
}

Running Seeders

Execute the seeder using:

php artisan db:seed --class=UsersTableSeeder

This command populates the users table with the specified data.

6. Migration and Seeding Together

You can also migrate and seed your database in one command:

php artisan migrate --seed

This is helpful for setting up a fresh database with all the necessary schema and data together.

Related Posts

How to Deploy a Laravel Project on a Linux Server Using HTDOCS and GitHub – Complete Step-by-Step Guide

Deploying a Laravel project on a Linux server becomes simple when you follow a structured process. In this guide, we will deploy a Laravel project by cloning Read More

Read More

How to Install and Enable GMP Extension in XAMPP PHP on Linux (Step-by-Step Guide)

Introduction If you are using XAMPP PHP on a Linux server and encounter errors like: then this usually means the GMP extension is not installed, not just Read More

Read More

How to Run an HTML Website Alongside a Laravel Project on the Same Server

Running a Laravel application is common for dynamic web platforms, dashboards, and APIs. However, many businesses and developers also want to run a simple HTML website on Read More

Read More
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x