Создание таблиц
Метод create
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('body');
$table->timestamps();
});
Всё, что внутри замыкания — определение колонок.
Типы колонок
id()— BIGINT AUTO_INCREMENT PRIMARY KEYbigIncrements('id')— то же самоеincrements('id')— INT AUTO_INCREMENTstring('name', 100)— VARCHAR(100)char('code', 4)— CHAR(4)text('body')— TEXTmediumText('body')— MEDIUMTEXTlongText('body')— LONGTEXTinteger('views')— INTbigInteger('count')— BIGINTsmallInteger('age')— SMALLINTtinyInteger('active')— TINYINTboolean('is_active')— BOOLEANdecimal('price', 8, 2)— DECIMAL(8,2)float('rating')— FLOATdouble('score')— DOUBLEdate('birthday')— DATEtime('starts_at')— TIMEdateTime('published_at')— DATETIMEtimestamp('created_at')— TIMESTAMPjson('options')— JSONjsonb('meta')— JSONB (PostgreSQL)uuid('uuid')— UUIDulid('ulid')— ULIDenum('status', ['draft', 'published'])— ENUM
Специальные колонки
timestamps()—created_atиupdated_atsoftDeletes()—deleted_atrememberToken()— для «запомнить меня»foreignId('user_id')— BIGINT для внешнего ключа
Проверка существования
if (Schema::hasTable('posts')) {
//
}
if (Schema::hasColumn('posts', 'title')) {
//
}
Удаление
Schema::drop('posts');
Schema::dropIfExists('posts');
Schema::dropColumns('posts', ['title', 'body']);
Переименование
Schema::rename('posts', 'articles');