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!

How to create a table in Laravel using migration

Creating a table in Laravel using migrations involves several steps. Here’s how you can do it:

1. Install Laravel (if you haven’t already)

First, you need Laravel installed on your machine. If you haven’t installed Laravel yet, you can use Composer to create a new Laravel project:

composer create-project --prefer-dist laravel/laravel yourProjectName

2. Set Up Your Environment

Ensure your .env file is configured with the correct database connection settings.

3. Generate a Migration File

You can generate a migration file using the Artisan CLI. The naming convention typically describes the actions being performed, such as creating a new table:

php artisan make:migration create_your_table_name

Replace your_table_name with the actual name of the table you want to create.

4. Define the Table Structure

Open the generated migration file in the database/migrations directory. You’ll see a class with up() and down() methods. Define your table structure in the up() method using the Laravel Schema builder:

Schema::create('your_table_name', function (Blueprint $table) {
    $table->id();  // Creates an auto-incrementing UNSIGNED BIGINT (primary key) equivalent column named "id".
    $table->string('name');  // Creates a VARCHAR equivalent column.
    $table->integer('age')->unsigned();  // Creates an UNSIGNED INT equivalent column.
    $table->timestamps();  // Creates 'created_at' and 'updated_at' columns.
});

5. Run the Migration

After defining your table, run the migration to apply the changes to your database:

php artisan migrate

This command will execute the up method in your migration file, which creates the table in your database.

6. Rolling Back

If you need to rollback the last batch of migrations, you can use:

php artisan migrate:rollback

This command will execute the down method of your migration class, which typically drops the table.

Additional Tips

  • Always test your migrations in a development environment before running them in production.
  • Use Laravel’s other migration commands like php artisan migrate:status to see the status of each migration.
  • Utilize Laravel’s Eloquent ORM to interact with your database through models.

By following these steps, you can effectively manage your database schema and easily rollback changes if necessary.

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