Skip to content

Instantly share code, notes, and snippets.

@cgiovanii
Last active September 1, 2018 23:24
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save cgiovanii/7481901 to your computer and use it in GitHub Desktop.
Save cgiovanii/7481901 to your computer and use it in GitHub Desktop.
laravel: Comandos migrate do artisan para criar tabelas

Criar tabelas com o artisan:migrate

php artisan migrate:make create_filmes_table --table=filmes --create

/app/database/migrations/xxx_create_filmes_table.php
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateFilmesTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('filmes', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('titulo_original', 150);
            $table->string('titulo_portugues', 150);
            $table->date('lancamento');
            $table->string('poster', 60);
            $table->text('sinopse');
            $table->smallInteger('avaliacao')->nullable();
            $table->string('imdb', 160);
            $table->integer('duracao');
            $table->smallInteger('discos');
            $table->integer('ano');
            $table->integer('tipo_id')->unsigned();

            $table->foreign('tipo_id')->references('id')->on('tipos')->on_delete('restrict');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('filmes');
    }

}
?>

#####Comandos para adicionar um campo a uma tabela existente via artisan onde comentario é o nome do campo e filmes é a tabela que vamos inserir o novo campo

php artisan migrate:make add_comentario_to_filmes_table --table=filmes

#####/app/database/migrations/xxx_add_comentario_to_filmes_table.php

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddComentarioToFilmesTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('filmes', function(Blueprint $table)
        {
            $table->text('comentario')->nullable(); //add o campo
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('filmes', function(Blueprint $table)
        {
            $table->dropColumn('comentario'); //apaga o campo
        });
    }

}
?>

#####Para dar carga as tabelas via artisan:seeds Temos que criar duas Models: Uma de Base e a Model da tabela que vamos dar a carga

#####/app/models/BaseModel.php

<?php

class BaseModel extends Eloquent
{
    public $timestamps = false; //Desabilita o campo automatico de timestamps do migrate
}

?>
/app/models/Genero.php
<?php

class Genero extends BaseModel
{
    protected $table = 'generos'; //Tabela que vamos dar a carga.
}
?>
E criamos o ficheiro seed com os dados da carga.
/app/database/seeds/GeneroTableSeeder.php
<?php

class GeneroTableSeeder extends Seeder
{

    public function run()
    {
        DB::table('generos')->delete();

        Genero::create(array(
            'descricao' => 'Ação'
        ));

        Genero::create(array(
            'descricao' => 'Aventura'
        ));

        Genero::create(array(
            'descricao' => 'Animação'
        ));

        Genero::create(array(
            'descricao' => 'Comédia'
        ));

        Genero::create(array(
            'descricao' => 'Crime'
        ));

        Genero::create(array(
            'descricao' => 'Documentário'
        ));

        Genero::create(array(
            'descricao' => 'Drama'
        ));
    }

}
?>

#####Temos que configurar o DatabaseSeeder para poder dar a carga a nossa tabela Genero:

<?php

class DatabaseSeeder extends Seeder {

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Eloquent::unguard();

        $this->call('GeneroTableSeeder'); //executa o nosso seeder
    }

}
?>

#####E por fim executamos o seguinte comando: php artisan db:seed

@reginaldojunior
Copy link

Estranho, para mim não funcionou esse exemplo... Porém tentei com esse aqui e deu certo

sudo php artisan make:migration table_example --create=TableExample

OBS: A versão do meu Laravel é a 5.1 💃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment