Setup Menus in Admin Panel

  • No products in the cart.

  • LOGIN

Migrations concepts

Laravel Migrations 


In Laravel, migrations provide mechanisms for creating and modifying database tables. Migrations are typically paired with Laravel’s schema builder to easily build your application’s database schema. If you have ever had to tell a teammate to manually add a column to their local database schema, you’ve faced the problem that database migrations solve. Migrations also allow you to roll back the most recent changes that you made to a database.

 

Generating Migrations in Laravel:

For creating a migration use the following command:

php artisan make:migration create_users_table

The new migration will be placed in your database/migrations directory.

Here, the –table and –create options are used to indicate the name of the table and whether the migration will be creating a new table.

 

Laravel Structure of Migration:

Migration class includes two methods namely:
1.up
2.down

up method:
The up method is used to add new tables, columns, or indexes to your database.

 

down method:
The down method should reverse the operations performed by the up method.

 

The below screenshot shows the migration structure:

 

 

Running Migrations in Laravel:

To perform the migrations perform the following artisan command:

php artisan migrate

This command will migrate all the tables which are in your database/migrations directory.

Note :

There might be a chance of losing your data. So, in order to protect and prevent the destruction of data the following command is used.
php artisan migrate --force

 

Rollback migrations:

Rollback command is used to perform the changes that are performed in the latest migration operation.
This command rolls back the last “batch” of migrations, which may include multiple migration files.
The following is the rollback command:

php artisan migrate:rollback

You can also rollback a limited number of migrations by providing the step option to the rollback command.
For example, the following command will rollback the last four migrations:

php artisan migrate:rollback --step=4

 

The migrate:reset command will roll back all of your application’s migrations:

php artisan migrate:reset

 

Rollback & Migrate in Single Command:

The following migrate:refresh command re-creates your entire database:

php artisan migrate:refresh
php artisan migrate:refresh --seed

 

Seeding in laravel:

Laravel seed method is used for seeding your database with test data using seed classes. Seeding means simply adding data to your tables. All seed classes are stored in the database/seeds directory.
To generate a seeder, execute the command make:seeder. All seeders generated by the framework will be placed in the database/seeds directory:

php artisan make:seeder UsersTableSeeder

A seeder class only contains one method by default: run. This method is called when the db:seed Artisan command is executed. Within the run method, you can insert data into your database according to your wish.

The below example shows that we had modified the UsersTablesseeder.php class:

 

 

Running Seeders:

Once you have written your seeder, you need to regenerate Composer’s autoloader using the following command:

composer dump-autoload

Now use the db:seed Artisan command to seed your database.

php artisan db:seed
php artisan db:seed --class=UsersTableSeeder

You can also seed your database using the migrate:refresh command, which will also rollback and re-run all of your migrations. This following command is useful for completely re-building your database:

php artisan migrate:refresh --seed

Attachments2

ASSIGNMENT : What is database migration. How to create migration via artisan ? MARKS : 10  DURATION : 10 minutes

ASSIGNMENT : Why are migrations necessary? MARKS : 10  DURATION : 10 minutes

SEE ALL Add a note
YOU
Add your Comment
 

Advanced Course Search Widget

Popular Courses

Course Categories

© PHPhelp. 2002 - 2018 All rights reserved.
X