Database: Migrations
Tables
Creating Tables
1 2 3 4 5 6
| Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email'); $table->timestamps(); });
|
Checking For Table / Column Existence
1 2 3 4 5 6 7
| if (Schema::hasTable('users')) { // The "users" table exists... }
if (Schema::hasColumn('users', 'email')) { // The "users" table exists and has an "email" column... }
|
Database Connection & Table Options
1 2 3 4 5 6 7
| Schema::connection('sqlite')->create('users', function (Blueprint $table) { $table->engine = 'InnoDB'; $table->charset = 'utf8mb4'; $table->collation = 'utf8mb4_unicode_ci'; $table->temporary(); $table->id(); });
|
Updating Tables
1 2 3
| Schema::table('users', function (Blueprint $table) { $table->integer('votes'); });
|
Renaming / Dropping Tables
1 2 3
| Schema::rename($from, $to); Schema::drop('users'); Schema::dropIfExists('users');
|
Columns
Creating Columns
1 2 3
| Schema::table('users', function (Blueprint $table) { $table->integer('votes'); });
|
Available Column Types
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| bigIncrements bigInteger binary boolean char dateTimeTz dateTime date decimal double enum float foreignId geometryCollection geometry id increments integer ipAddress json jsonb lineString longText macAddress mediumIncrements mediumInteger mediumText morphs multiLineString multiPoint multiPolygon nullableMorphs nullableTimestamps nullableUuidMorphs point polygon rememberToken set smallIncrements smallInteger softDeletesTz softDeletes string text timeTz time timestampTz timestamp timestampsTz timestamps tinyIncrements tinyInteger tinyText unsignedBigInteger unsignedDecimal unsignedInteger unsignedMediumInteger unsignedSmallInteger unsignedTinyInteger uuidMorphs uuid year
|
1 2 3 4 5 6 7 8 9
| // Updating Column Attributes $table->string('name', 50)->nullable()->change();
// Renaming Columns $table->renameColumn('from', 'to');
// Dropping Columns $table->dropColumn('votes'); $table->dropColumn(['votes', 'avatar', 'location']);
|
Indexes
1 2 3 4 5 6 7 8
| // Creating Indexes $table->string('email')->unique();
// create the index after defining the column $table->unique('email');
// specify the index name $table->unique('email', 'unique_email');
|