Session 07
Muhammad Rizwan Arshad,
Principal Software Engineer, Nextbridge.
August 05, 2015.
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
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');
}
}
# php artisan migrate
If "class not found", run: composer dump-autoload
# php artisan migrate --force
# php artisan migrate:rollback
To rollback all migrations: # php artisan migrate:reset
# php artisan migrate:refresh
# php artisan migrate:refresh --seed
Schema::create('users', function ($table) { $table->increments('id'); });
if (Schema::hasTable('users')) { // } if (Schema::hasColumn('users', 'email')) { // }
Schema::connection('foo')->create('users', function ($table) {
$table->increments('id');
});
Schema::create('users', function ($table) {
$table->engine = 'InnoDB';
$table->increments('id');
});
Schema::rename($from, $to);
Schema::drop('users');
Schema::dropIfExists('users');
Schema::table('users', function ($table) {
$table->string('email');
});
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 |
Schema::table('users', function ($table) {
$table->string('name', 50)->change();
});
Schema::table('users', function ($table) {
$table->renameColumn('from', 'to');
});
Schema::table('users', function ($table) {
$table->dropColumn('votes');
});
Schema::table('users', function ($table) {
$table->dropColumn(['votes', 'avatar', 'location']);
});
$table->string('email')->unique();
$table->unique('email');
$table->index(['account_id', 'created_at']);
$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. |
$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. |
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');
# 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'),
]);
}
}
# php artisan db:seed
# php artisan db:seed --class=UserTableSeeder
# php artisan migrate:refresh --seed