xxxxxxxxxx
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateProductsTable extends Migration
{
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->decimal('price', 8, 2);
$table->timestamps();
});
// Insert default data
\DB::table('products')->insert([
['name' => 'Default Product 1', 'price' => 10.00, 'created_at' => now(), 'updated_at' => now()],
['name' => 'Default Product 2', 'price' => 20.00, 'created_at' => now(), 'updated_at' => now()],
]);
}
public function down()
{
Schema::dropIfExists('products');
}
}
xxxxxxxxxx
php artisan make:migration add_paid_to_users_table --table=users
xxxxxxxxxx
public function up()
{
// Create the table
Schema::create('users', function($table){
$table->increments('id');
$table->string('email', 255);
$table->string('password', 64);
$table->boolean('verified');
$table->string('token', 255);
$table->timestamps();
});
// Insert some stuff
DB::table('users')->insert(
array(
'email' => 'name@domain.com',
'verified' => true
)
);
}
xxxxxxxxxx
php artisan make:migration add_company_id_to_users_table
//in up() method
Schema::table('users', function (Blueprint $table) {
$table->unsignedBigInteger('company_id');
});
//in down() method
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('company_id');
});
xxxxxxxxxx
//to create migration file in PHP use the artisan command "make"
php artisan make:migration create_users_table
// migration file must follow the naming convention "operation_tableName_table"
//Migration file to add column naming convention would be "add_tablename_table"
xxxxxxxxxx
composer require doctrine/dbal
php artisan make:migration add_end_at_filed_to_appointment_table
xxxxxxxxxx
To Generate Laravel Migrations from an existing database.
Use the following package.
https://github.com/Xethron/migrations-generator