Web Development
28-08-2025

PSQL querries based on Laravel migrations

Dmytro Tus
Full Stack Web developer

Sometimes we need to run clean querries in database, but the most of the time I'm working with database through ORM like eloquent.

Let's write an example of clean database querries based on Laravel migrations

## PHP
Schema::table('customers', function (Blueprint $table) {
      $table->uuid('new_column')->unique()->nullable();
});

## PSQL
ALTER TABLE customers
ADD COLUMN new_column UUID NULL;

-- Create a unique index (Postgres doesn't allow both unique & separate index automatically)
CREATE UNIQUE INDEX customers_wms_id_unique ON customers(new_column);

Another example

## PHP
Schema::table('customers', function (Blueprint $table) {
       $table->string('invoice_type', 16)
           ->default('personal')
           ->index();
});

## PSQL
ALTER TABLE customers
ADD COLUMN invoice_type VARCHAR(16) DEFAULT 'personal';

CREATE INDEX customers_invoice_type_index 
    ON customers (invoice_type);

Happy coding 👨‍💻

 

 

Wallpaper by https://unsplash.com/@paolobendandi


Another posts