Last active
October 30, 2021 06:51
-
-
Save EhtuCom/79f975d9f61c0a58a5df69776ca8d340 to your computer and use it in GitHub Desktop.
Laravel Eloquent Migrations Models Database Cheat Sheet by Ehtu.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
php artisan make:migration create_users_table --create=users | |
php artisan make:migration add_votes_to_users_table --table=users | |
... | |
php artisan migrate | |
php artisan migrate:rollback | |
php artisan migrate:rollback --step=5 | |
TO CAMEL CASE MODEL: model_name "_" characteer becomes uppercase |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Command Description | |
Daily use: | |
$table->binary('data'); BLOB equivalent column. | |
$table->boolean('confirmed'); BOOLEAN equivalent column. | |
$table->char('name', 100); CHAR equivalent column with an optional length. | |
$table->date('created_at'); DATE equivalent column. | |
$table->decimal('amount', 8, 2); DECIMAL equivalent column with a precision (total digits) and scale (decimal digits). | |
$table->double('amount', 8, 2); DOUBLE equivalent column with a precision (total digits) and scale (decimal digits). | |
$table->geometry('positions'); GEOMETRY equivalent column. | |
$table->integer('votes'); INTEGER equivalent column. | |
$table->ipAddress('visitor'); IP address equivalent column. | |
$table->json('options'); JSON equivalent column. | |
$table->longText('description'); LONGTEXT equivalent column. | |
$table->softDeletes(); Adds a nullable deleted_at TIMESTAMP equivalent column for soft deletes. | |
$table->string('name', 100); VARCHAR equivalent column with a optional length. | |
$table->text('description'); TEXT equivalent column. | |
$table->tinyInteger('votes'); TINYINT equivalent column. | |
$table->year('birth_year'); YEAR equivalent column. | |
All: | |
$table->bigIncrements('id'); Auto-incrementing UNSIGNED BIGINT (primary key) equivalent column. | |
$table->bigInteger('votes'); BIGINT equivalent column. | |
$table->binary('data'); BLOB equivalent column. | |
$table->boolean('confirmed'); BOOLEAN equivalent column. | |
$table->char('name', 100); CHAR equivalent column with an optional length. | |
$table->date('created_at'); DATE equivalent column. | |
$table->dateTime('created_at'); DATETIME equivalent column. | |
$table->dateTimeTz('created_at'); DATETIME (with timezone) equivalent column. | |
$table->decimal('amount', 8, 2); DECIMAL equivalent column with a precision (total digits) and scale (decimal digits). | |
$table->double('amount', 8, 2); DOUBLE equivalent column with a precision (total digits) and scale (decimal digits). | |
$table->enum('level', ['easy', 'hard']); ENUM equivalent column. | |
$table->float('amount', 8, 2); FLOAT equivalent column with a precision (total digits) and scale (decimal digits). | |
$table->geometry('positions'); GEOMETRY equivalent column. | |
$table->geometryCollection('positions'); GEOMETRYCOLLECTION equivalent column. | |
$table->increments('id'); Auto-incrementing UNSIGNED INTEGER (primary key) equivalent column. | |
$table->integer('votes'); INTEGER equivalent column. | |
$table->ipAddress('visitor'); IP address equivalent column. | |
$table->json('options'); JSON equivalent column. | |
$table->jsonb('options'); JSONB equivalent column. | |
$table->lineString('positions'); LINESTRING equivalent column. | |
$table->longText('description'); LONGTEXT equivalent column. | |
$table->macAddress('device'); MAC address equivalent column. | |
$table->mediumIncrements('id'); Auto-incrementing UNSIGNED MEDIUMINT (primary key) equivalent column. | |
$table->mediumInteger('votes'); MEDIUMINT equivalent column. | |
$table->mediumText('description'); MEDIUMTEXT equivalent column. | |
$table->morphs('taggable'); Adds taggable_id UNSIGNED BIGINT and taggable_type VARCHAR equivalent columns. | |
$table->multiLineString('positions'); MULTILINESTRING equivalent column. | |
$table->multiPoint('positions'); MULTIPOINT equivalent column. | |
$table->multiPolygon('positions'); MULTIPOLYGON equivalent column. | |
$table->nullableMorphs('taggable'); Adds nullable versions of morphs() columns. | |
$table->nullableTimestamps(); Alias of timestamps() method. | |
$table->point('position'); POINT equivalent column. | |
$table->polygon('positions'); POLYGON equivalent column. | |
$table->rememberToken(); Adds a nullable remember_token VARCHAR(100) equivalent column. | |
$table->set('flavors', ['strawberry', 'vanilla']); SET equivalent column. | |
$table->smallIncrements('id'); Auto-incrementing UNSIGNED SMALLINT (primary key) equivalent column. | |
$table->smallInteger('votes'); SMALLINT equivalent column. | |
$table->softDeletes(); Adds a nullable deleted_at TIMESTAMP equivalent column for soft deletes. | |
$table->softDeletesTz(); Adds a nullable deleted_at TIMESTAMP (with timezone) equivalent column for soft deletes. | |
$table->string('name', 100); VARCHAR equivalent column with a optional length. | |
$table->text('description'); TEXT equivalent column. | |
$table->time('sunrise'); TIME equivalent column. | |
$table->timeTz('sunrise'); TIME (with timezone) equivalent column. | |
$table->timestamp('added_on'); TIMESTAMP equivalent column. | |
$table->timestampTz('added_on'); TIMESTAMP (with timezone) equivalent column. | |
$table->timestamps(); Adds nullable created_at and updated_at TIMESTAMP equivalent columns. | |
$table->timestampsTz(); Adds nullable created_at and updated_at TIMESTAMP (with timezone) equivalent columns. | |
$table->tinyIncrements('id'); Auto-incrementing UNSIGNED TINYINT (primary key) equivalent column. | |
$table->tinyInteger('votes'); TINYINT equivalent column. | |
$table->unsignedBigInteger('votes'); UNSIGNED BIGINT equivalent column. | |
$table->unsignedDecimal('amount', 8, 2); UNSIGNED DECIMAL equivalent column with a precision (total digits) and scale (decimal digits). | |
$table->unsignedInteger('votes'); UNSIGNED INTEGER equivalent column. | |
$table->unsignedMediumInteger('votes'); UNSIGNED MEDIUMINT equivalent column. | |
$table->unsignedSmallInteger('votes'); UNSIGNED SMALLINT equivalent column. | |
$table->unsignedTinyInteger('votes'); UNSIGNED TINYINT equivalent column. | |
$table->uuid('id'); UUID equivalent column. | |
$table->year('birth_year'); YEAR equivalent column. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Database\Migrations\Migration; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Support\Facades\Schema; | |
class CreateUsersTable extends Migration | |
{ | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('users', function (Blueprint $table) { | |
$table->id(); | |
$table->string('name'); | |
$table->string('email')->unique(); | |
$table->timestamp('email_verified_at')->nullable(); | |
$table->string('password'); | |
$table->rememberToken(); | |
$table->foreignId('current_team_id')->nullable(); | |
$table->text('profile_photo_path')->nullable(); | |
$table->timestamps(); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Schema::dropIfExists('users'); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment