PHP code example of jorgemddev / kumbiaphp-migrations
1. Go to this page and download the library: Download jorgemddev/kumbiaphp-migrations library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jorgemddev / kumbiaphp-migrations example snippets
class CreateProductsTable extends Migration
{
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('sku', 100)->unique();
$table->decimal('price', 10, 2);
$table->integer('stock')->default(0);
$table->boolean('is_active')->default(true);
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('products');
}
}
class AddDescriptionToProductsTable extends Migration
{
public function up()
{
Schema::table('products', function (Blueprint $table) {
$table->text('description')->nullable()->after('name');
$table->string('image')->nullable();
});
}
public function down()
{
Schema::table('products', function (Blueprint $table) {
$table->dropColumn(['description', 'image']);
});
}
}
class FixScheduleExemptDefault extends Migration
{
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->boolean('schedule_exempt')->default(false)->change();
});
}
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->boolean('schedule_exempt')->change();
});
}
}
class CreateOrdersTable extends Migration
{
public function up()
{
Schema::create('orders', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->enum('status', ['pending', 'paid', 'cancelled'])->default('pending');
$table->decimal('total', 10, 2);
$table->timestamps();
$table->softDeletes();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
});
}
public function down()
{
Schema::dropIfExists('orders');
}
}
// app/database/seeds/DatabaseSeeder.php
class DatabaseSeeder extends Seeder
{
public function run()
{
$this->call([
UsersSeeder::class,
CategoriesSeeder::class,
ProductsSeeder::class,
]);
}
}
// Insertar filas
$this->insert('tabla', $filas);
// Vaciar tabla
$this->truncate('tabla');
// SQL personalizado
$this->query('UPDATE config SET value = ? WHERE key = ?', ['activo', 'status']);
// Llamar a otro seeder
$this->call(OtroSeeder::class);
$this->call([SeederA::class, SeederB::class]);
bash
php app/bin/migrate --install
bash
php app/bin/migrate --install
bash
# Ejecutar migraciones pendientes
php app/bin/migrate
# Ver estado de todas las migraciones
php app/bin/migrate --status
# Revertir el último lote de migraciones
php app/bin/migrate --rollback
# Revertir todas las migraciones
php app/bin/migrate --reset
# Revertir todo y volver a ejecutar
php app/bin/migrate --refresh
# Crear la tabla de control (solo la primera vez)
php app/bin/migrate --install
# Ayuda
php app/bin/migrate --help
bash
# Migración en blanco
php app/bin/migrate make:migration nombre_descriptivo
# Para crear una tabla nueva
php app/bin/migrate make:migration create_products_table --create=products
# Para modificar una tabla existente
php app/bin/migrate make:migration add_price_to_products --table=products