Laravel Migration

      • Note: when creating a table with automatically increasing id, for example:
         $table->increments('id');

        The type created in a Mysql table is an unsigned integer (i.e., int(10), signed integer in mysql is int(11)).  If this id is used in another table as a foreign key, don’t forget to create the corresponding field as an unsigned integer as well. Otherwise, the foreign key constraint will fail with Mysql error 150.

      • Rollback one step (Laravel 5.3 or above)
        php artisan migrate:rollback --step=1

        To roll back a particular batch, first, change the batch number in the migration table to be one greater than the latest number. Second, run the rollback command above.

      • To execute latest migration:
        php artisan migrate
      • To create a table
        php artisan make:migration create_table_name
      • To make model (note: don’t forget to modify namespace if file is moved to any other directory)
        php artisan make:model TableName
      • Combining create table with make model:
        php artisan make:model TableName --migration
      • When creating a migration file in Laravel, the class name is generated in association with the file name. If one change the class name in a way that does not follow the Laravel convention, one would get an fatal error that states: Class ‘so and so’ not found. This error is not fixable by doing composer dump-autoload. It can only be fixed by changing the name back to what Laravel expects
      • If php artisan tinker is not loading the right namespace for the model, do a
        composer dump-autoload
      • php artisan migrate –pretend

        this command will not run the migration rather it will print out it’s raw SQL

      • If MySQL ALGORITHEM parameter needed to be used in migration, use the raw DB commands. For example, instead of doing:
        Schema::table('users', function (Blueprint $table) {
                    $table->renameColumn('cars', 'fast_cars');
                });

        Do this instead:

        \DB::statement('ALTER TABLE test CHANGE car fast_car VARCHAR(10) NOT NULL, ALGORITHM=INPLACE, LOCK=NONE;');