Alter Tables in Laravel with change method

Last Updated at : March 7, 2018 By - Usama Muneer | 6167

    Laravel ships in with the awesomeness of Database Migrations which allows you to version control your database. But there are number of events when you are required to change the datatypes or attributes assigned to your table columns inside of your migrations. Well Laravel got you covered, you can achieve it by the change() helper method.

    We’ll look at some scenarios on how we can utilise this change() method in our database migrations.

    Scenario # 01 : Nullable fields

    You’re required to make a field nullable and didn’t want to break your database and previous records. You can simply create a migration for that likeso,

    php artisan make:migration add_nullable_constraint_to_my_table --table=my_table

    Inside of your migration, you need to rewrite the field you’re referring to and add the change method on it with the alterations.

    //up 
    Schema::table('my_table', function (Blueprint $table) {
     $table->string( 'name' )->nullable()->change();
    });

    And for the down method, you can do something like this.

    //down
    Schema::table('my_table', function (Blueprint $table) {
     $table->string('name')->nullable(false)->change();
    });

    Scenario # 02: Data Length

    What if you need to change the data type’s length for some field? For instance, we want to increase the field maximum allowed size from 10 to 50. Well change() is here for the rescue.

    Its always a good practice for creating separate migrations for these alterations so that it would not conflict.

    $table->string( 'name',50 )->change();

    Scenario # 03: Change Datatypes

    Another scenario is where we need to change the datatype of the field, lets say from string to multiLineString. We can do it with the change method.

    Add a new migration and migrate for the changes to take effect.

    $table->multiLineString('name')->change();

    Bonus Tip

    You may call change() method before or after the alteration method you want to apply & it will work the same. For example

    $table->string( 'name' )->change()->nullable();

    Will work the same as

    $table->string( 'address' )->nullable()->change();

    Conclusion

    If you have any other real world scenarios in mind, let us know in the comments section below & we’ll get back to it with the solution. You can also follow us on Twitter.

    blog user
    Usama Muneer

    A web enthusiastic, self-motivated & detail-oriented professional Full-Stack Web Developer from Karachi, Pakistan with experience in developing applications using JavaScript, WordPress & Laravel specifically. Loves to write on different web technologies with an equally useful skill to make some sense out of it.

    Related Posts

    Imagine you have a scenario where you need to add multiple actions to your Laravel Notification where you can add as many as button components with different URLs.

    Importing Excel files come handy when you’re building some large scale inventory kind of an application where you need to import set of data from CSV or excel files. In this short tutorial, we’ll go through on importing excel files with Laravel & look on for some crazy hacks and tricks in between.

    Gear up fellow developers, another Laravel Meetup is coming back to Lahore & it will be a great milestone while helping our fellow web developers across the city to become motivated and will help them grow as Laravel developers.

    Getting an app developed is not the only thing that one needs to ensure in terms of digital visibility and e-commerce. Yes, having an app is important but to stay updated and to be abreast with the competition Optimization of performance is necessary.