All the developers of any team should not have access to the database. This is why Laravel provides a powerful migration system that you use to create/update, or delete anything. So you can realize how important this thing is. I think this will be very important to you in the Laravel migration tutorial. Let’s start with this thing.
Making Laravel migration
The fundamental part of our Laravel migration file tutorial
is creating this. Usually, we can do this manually. But the best practice is
making files via command. It will save you time and make things more
straightforward.
Basic migration creation
php artisan
make:migration myMigration
This is going to provide you with a fundamental migration
file. There will be no schema and nothing. All you need to manage yourself.
Creating migration with schema method:
php artisan
make:migration create_mymigration
It would help if you used “create_” before the migration
file name. It will make a schema by default. This schema will hold a create
method.
Properties of Laravel migration
In most common cases, people are getting migration far confusing kinds of stuff. But trust me, and this is the easiest thing to deal with in larvae. Here we have listed all the necessary properties of Laravel migration. I hope those were going to help you out.
- $table->id();
- $table->integer('user_id')->unsigned();
- $table->string('slug');
- $table->integer('company')->unsigned();
- $table->enum('types',["fullTime","partTime","remote","intern","contractual"])->default('fullTime');
- $table->string('label')->nullable();
- $table->longText('job_details')->nullable();
- $table->integer('industry')->unsigned()->nullable();
- $table->integer('functional_area')->unsigned();
- $table->integer('child_functional_area')->unsigned()->nullable();
- $table->json('tags')->nullable();
- $table->integer('min_salary')->nullable();
- $table->text('map_location')->nullable();
- $table->integer('min_experience')->default(0);
- $table->enum('status', ['live', 'hold', 'suspend', 'draft', 'pending', 'deleted'])->default('live');
- $table->integer('show_count')->default(0);
- $table->date('declined_date')->nullable();
- $table->string('location')->nullable();
- $table->integer('division_id')->nullable();
- $table->boolean('is_published')->default(0);
- $table->timestamps();
Make sure your migration must have the timestamps unless it
will cause you a problem when trying to update data via Laravel CRUD operation.
Besides that all those commands, there are some more commands available. Here
we provide the standard migration command.
Manipulate existing database property by migration
If you want to manipulate database property, like deleting or
updating any database table or column, then this is possible by Laravel
migration. If you wish to update Laravel database essential property, you must ensure
that the Doctrine Dbal package is installed. This is required.
Install doctrine DBAL
: composer require doctrine/dbal
How do you Remove or rename a datable table by Laravel migration?
First of all,
make a migration file. And go to the “up” method. Here it would help if you wrote
things about the table like that -
Schema::rename("Old table name","new table name");
How to Rename or remove the column by Laravel migration?
Often we need to modify our existing Laravel database
columns. In this case, we also need to use the table function instead of create
function.
Schema::table('demo', function(Blueprint $table){
$table->renameColumn('roll','newroll');
});
Run the migration
After making a migration file, you just need to run it.
After running the command, it will make or update your database. Here I am
going to note out all those commands. I hope those are going to be helpful
enough for you.
·
Make all the database table : php artisan migrate
·
Drop all tables and create again : php artisan migrate:fresh
·
Run the “down()” method of migration : php artisan migrate:refresh
·
Just remove all the database tables: php artisan db:wipe
·
Rolling back the migration : php artisan migrate:reset
·
Check the current status : php artisan migrate:status
So here are all the things from my side about migration. If you want to know anything else about migration, let me know. Even if you think anything is wrong in the Laravel migration tutorial, mention me in the comment. We will solve those soon. We appreciate people to comment their opinion. Happy programming!