'default' => env('DB_CONNECTION', 'mysql'),
quiere decir que busca en el archivo .env la variable DB_CONNECTION
en nuestro caso es igual a mysql...
si no existiera esa variable toma el valor del 2do parametro 'mysql'
en .env debemos definir esa y otras variables
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db_blog
DB_USERNAME=root
DB_PASSWORD=
las migraciones es una suerte de control de versiones de nuestra base de datos,
estas se encuentran en database/migrations
por default Laravel viene con 2 migraciones de users y passwords.
cada migracion tiene 2 funciones up() y down()
veamos la up() de users
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
donde $table->rememberToken()
es lo mismo que escribir (ver metodo en Blueprint)
$this->string('remember_token', 100)->nullable();
y donde
$table->timestamps();
es lo mismo que escribir:
$this->timestamp('created_at', $precision)->nullable();
$this->timestamp('updated_at', $precision)->nullable();
para ejecutar todas estas migraciones desde la consola escribimos:
php artisan migrate
para hacer un rollback (o llamar a las funciones down() de todas las migraciones que se ejecutaron en el ultimo migrate) desde la consola escribimos:
php artisan migrate:rollback
para crear una migration usamos
php artisan make:migration create_messages_table
esto nos creara un archivo con la fecha y hora p.e 2018_03_21_122354_create_messages_table.php
con los metodos vacios, si queremos generar automaticamente un archivo de migration para la creacion de una tabla hacemos
php artisan make:migration create_messages_table --create=messages
ahora si, el archivo creado tiene implementado minimamente la funcion up()
public function up()
{
Schema::create('messages', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}
y lo completamos con los datos que queremos
public function up()
{
Schema::create('messages', function (Blueprint $table) {
$table->increments('id');
$table->string('nombre');
$table->string('email');
$table->text('mensaje');
$table->timestamps();
});
}
para ejecutar la migration
php artisan migrate
si queremos agregar un campo nuevo
php artisan make:migration add_telefono_messages_table --table=messages
ahora la funcion up del archivo de migration tiene la sgt forma:
public function up()
{
Schema::table('messages', function (Blueprint $table) {
//
});
}
ahora agregamos el campo nuevo y en la funcion down() la sentencia contraria, osea la eliminacion de ese campo:
public function up()
{
Schema::table('messages', function (Blueprint $table) {
$table->string('telefono')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('messages', function (Blueprint $table) {
$table->dropColumn('telefono');
});
}
tip: para recrear totalmente tu base de datos se usa
php artisan migrate:refresh
para hacer migratios que modifican/eliminan columnas de la tabla debemos instalar antes doctrine/dbal asi:
composer require doctrine/dbal
*******************
https://laravel.com/docs/5.6/migrations
campos disponibles para migrations
Command | Description |
---|---|
$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 INTEGER 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->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. |
No hay comentarios:
Publicar un comentario