Laravel 5.1

Session 07

Muhammad Rizwan Arshad,

Principal Software Engineer, Nextbridge.

August 05, 2015.

Database Migrations

Generating Migrations

To create a migration, use the make:migration Artisan command:

php artisan make:migration create_users_table

 

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

php artisan make:migration add_votes_to_users_table --table=users

php artisan make:migration create_users_table --create=users

Migration Structure

class CreateFlightsTable extends Migration
{
    public function up()
    {
        Schema::create('flights', function (Blueprint $table){
            $table->increments('id');
            $table->string('name');
            $table->string('airline');
            $table->timestamps();
        });
    }
    public function down(){
        Schema::drop('flights');
    }
}

Running Migrations

# php artisan migrate

If "class not found", run: composer dump-autoload

 

Forcing Migrations To Run In Production

# php artisan migrate --force

Rolling Back Migrations

# php artisan migrate:rollback

To rollback all migrations: # php artisan migrate:reset

Rollback / Migrate In Single Command

# php artisan migrate:refresh

 php artisan migrate:refresh --seed

Writing Migrations

Creating Tables

Schema::create('users', function ($table) {
    $table->increments('id');
});

Checking For Table / Column Existence

if (Schema::hasTable('users')) {
    //
}

if (Schema::hasColumn('users', 'email')) {
    //
}

Connection & Storage Engine

Schema::connection('foo')->create('users', function ($table) {
    $table->increments('id');
});
Schema::create('users', function ($table) {
    $table->engine = 'InnoDB';
    $table->increments('id');
});

Renaming / Dropping Tables

Schema::rename($from, $to);

Schema::drop('users');

Schema::dropIfExists('users');

Creating Columns

Schema::table('users', function ($table) {
    $table->string('email');
});

Column Modifiers

Schema::table('users', function ($table) {
    $table->string('email')->nullable();
});

ModifierDescription

->first() Place the column "first" in the table (MySQL Only)
->after('column') Place the column "after" another column (MySQL Only)
->nullable() Allow NULL values to be inserted into the column
->default($value) Specify a "default" value for the column
->unsigned() Set integer columns to UNSIGNED

Updating Column Attributes

Schema::table('users', function ($table) {
    $table->string('name', 50)->change();
});

Renaming Columns

Schema::table('users', function ($table) {
    $table->renameColumn('from', 'to');
});

Dropping Columns

Schema::table('users', function ($table) {
    $table->dropColumn('votes');
});
Schema::table('users', function ($table) {
    $table->dropColumn(['votes', 'avatar', 'location']);
});

Creating Indexes

$table->string('email')->unique();
$table->unique('email');
$table->index(['account_id', 'created_at']);

Available Index Types

$table->primary('id'); Add a primary key.
$table->primary(['first', 'last']); Add composite keys.
$table->unique('email'); Add a unique index.
$table->index('state'); Add a basic index.

Dropping Indexes

 
$table->dropPrimary('users_id_primary'); Drop a primary key from the "users" table.
$table->dropUnique('users_email_unique'); Drop a unique index from the "users" table.
$table->dropIndex('geo_state_index'); Drop a basic index from the "geo" table.

Foreign Key Constraints

Schema::table('posts', function ($table) {
    $table->integer('user_id')->unsigned();
    $table->foreign('user_id')->references('id')->on('users');
});
$table->foreign('user_id')
      ->references('id')->on('users')
      ->onDelete('cascade');
$table->dropForeign('posts_user_id_foreign');

Database: Seeding

Writing Seeders

# php artisan make:seeder UserTableSeeder
class DatabaseSeeder extends Seeder
{
    public function run()
    {
        DB::table('users')->insert([
            'name' => str_random(10),
            'email' => str_random(10).'@gmail.com',
            'password' => bcrypt('secret'),
        ]);
    }
}

Running Seeders

 
# php artisan db:seed

# php artisan db:seed --class=UserTableSeeder
# php artisan migrate:refresh --seed
 

Thanks !!!

Laravel 5.1 - Session 07

By gr8rizwan

Laravel 5.1 - Session 07

  • 1,305