Thursday, April 25, 2024
HomePHPLaravel Migrations: A Newbie's Information

Laravel Migrations: A Newbie’s Information


On this article, we’ll discover the fundamentals of Laravel migrations and how you can use them successfully. Migrations are like model management to your database, permitting your group to outline and share the applying’s database schema definition.

Common PHP internet software framework Laravel presents quite a lot of options and instruments for creating cutting-edge and dependable internet apps.

Its built-in help for database migrations, which helps builders to handle database adjustments and updates in a logical and arranged style, is one among Laravel’s core options.

On this put up, we’ll study Laravel migration in additional element and focus on the way it can enhance the way in which you deal with database schema adjustments.

What’s Laravel Migration?

Laravel migration is a function that permits builders to outline and handle database schema adjustments in a structured and repeatable manner. You should utilize Laravel migration to create, modify, or delete database tables, columns, indexes, and different schema components utilizing easy PHP code.

Earlier than the go to the deep dive into Laravel migration, let’s perceive some frequent phrases of migrations:

  1. Schema: Laravel migration makes use of the schema builder to create, modify or delete database tables.
  2. Artisan: Laravel’s command-line interface (CLI) that permits builders to generate and handle migrations.
  3. Up and down: Laravel migration has two principal strategies – up and down. The up methodology is used to outline the adjustments to be made to the database schema, whereas the down methodology specifies how you can undo these adjustments.
  4. Rollback: Laravel migration permits builders to undo essentially the most lately utilized migration utilizing the rollback command.
  5. Overseas keys: Laravel migration permits the definition of overseas keys and their relationships with different tables within the database.
  6. Indexes: Laravel migration permits builders to outline indexes for columns of their database tables, which helps to hurry up queries.
  7. Constraints: Laravel migration permits the definition of constraints, resembling distinctive or not null, on columns in database tables.
  8. Migrate: Laravel’s Artisan command to use all pending migrations to the database.
  9. Migration standing: Laravel migration permits builders to examine the standing of migrations utilizing the Artisan command migrate:standing, which shows a listing of all migrations and their present standing.
  10. Seeding: Laravel migration permits builders to seed their database with preliminary knowledge utilizing seeders, that are courses that outline knowledge to be inserted into the database through the migration course of.

Making a Migration in Laravel

The make:migration Artisan command in Laravel can be utilized to construct a brand new migration by producing a brand new migration file within the database/migrations listing. Two strategies—up and down—that specify the actions to be taken when the migration is utilized or rolled again, respectively, are included within the migration file.

Sponsored Hyperlinks

Let’s create a customers desk utilizing the migration command:

php artisan make:migration create_users_table

This can generate a brand new migration file with a reputation like 2023_04_09_000000_create_users_table.php into database/migrations listing. You’ll be able to then open the file and outline the up and down strategies like this:

public operate up()
{
    Schema::create('customers', operate (Blueprint $desk) {
        $table->id();
        $table->string('identify');
        $table->string('electronic mail')->distinctive();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

public operate down()
{
    Schema::dropIfExists('customers');
}

The up() methodology creates a brand new customers desk with columns, whereas the down methodology drops the desk if it exists.

You’ll be able to then run the migration utilizing the migrate Artisan command:

php artisan migrate

As soon as the run the above command, It’ll create the customers desk in your database.

Learn how to replace Migration in Laravel

Generally, That you must modify an present migration. You’ll be able to create a brand new migration that modifies the schema and runs it after the unique migration. Let’s replace our customers desk and add a brand new column right here, The brand new migration like this:

php artisan make:migration add_role_to_users_table

A brand new migration file with the identify 2023_04_09_000001_add_role_to_users_table.php might be created consequently. The up and down strategies can then be outlined by opening the file as follows:

public operate up()
{
    Schema::desk('customers', operate (Blueprint $desk) {
        $table->string('position')->default('consumer');
    });
}

public operate down()
{
    Schema::desk('customers', operate (Blueprint $desk) {
        $table->dropColumn('position');
    });
}

The up() operate provides a brand new position column with the default worth of consumer to the customers desk, whereas the down() methodology removes the column from the desk.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments