Skip to content

Instantly share code, notes, and snippets.

@marcoszf
Created March 11, 2016 18:05
Show Gist options
  • Select an option

  • Save marcoszf/2ad8f0b87bf3b6ac0894 to your computer and use it in GitHub Desktop.

Select an option

Save marcoszf/2ad8f0b87bf3b6ac0894 to your computer and use it in GitHub Desktop.
Schema Builder
"http://laravel.com/docs/schema" Documentação oficial
A classe SCHEMA fornecida pelo laravel fornece um meio de manipular a base de dados
<?php
#criar uma nova tabela 'users'
Schema::create('users', function($table)
{
$table->increments('id');
});
#renomear uma tabela existente
Schema::rename($from, $to);
#Apagar uma tabela
Schema::drop('users');
#ou apagar se existir
Schema::dropIfExists('users');
/** Adicionando colunas **/
#Para atualizar uma tabela existente, utilize o método Schema::tabke
Schema::table('users', function($table)
{
$table->string('email');
});
//Tipos de colunas que podem ser utilizadas na construção do esquema das tabelas:
//Lista dos mais comuns que utilizarei com frequência e suas Equivalências comentadas:
//Numéricos
$table->increments('id'); #ID da tabela (chave primária), com auto incremento
$table->boolean('confirmado'); #BOOLEAN
$table->bigInteger('votes'); #BIGINT
$table->decimal('amount', 10, 2); #DECIMAL com precisão e escala
$table->double('column', 15, 8); #double com precisão
$table->float('amount'); #FLOAT
$table->integer('votes'); #Integer
$table->smallInteger('votes'); #SMALLINT
//Literáis
$table->string('name', 100); #VARCHAR com length
$table->text('description'); #TEXT
$table->char('name', 4); #CHAR com length
$table->enum('choices', array('foo', 'bar')); #ENUM
$table->longText('description'); #LONGTEXT
//Datas
$table->date('created_at'); #DATE
$table->dateTime('created_at'); #DATETIME
$table->timestamp('added_on'); #TIMESTAMP
$table->nullableTimestamps(); #mesmo que timestamps() porém permite NUULs
$table->timestamps(); #Adiciona as colunas created_at e updated_at
//Outros
->nullable() #permitir NULL à coluna
->default($value) #declara um valor padrão para a coluna
->unsigned() #define o Integer para UNSIGNED
->after('coluna'); #especifica a ordem das colunas
#Renomear uma coluna
Schema::table('users', function($table)
{
$table->renameColumn('De', 'Para');
});
#Deletando uma coluna
Schema::table('users', function($table)
{
$table->dropColumn('usuario');
});
#Deletando muitas colunas em uma tabela
Schema::table('users', function($table)
{
$table->dropColumn(array('usuario', 'nome', 'email'));
});
//Verificando a existência da coluna ou tabela
#verificando a existência da tabela
if (Schema::hasTable('users')){ }
#verificando a existência das colunas
if (Schema::hasColumn('users', 'email')){ }
/**Adicionando Indexes**/
//Há dois modos de adicionar os index
#na definição da coluna
$table->string('id')->unique();
#separado
$table->primary('id'); #adiciona chave primária
$table->primary(array('first', 'last')); #chaves compostas
$table->unique('email'); #index único
$table->index('state'); #index básico
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment