3

I am trying to create enum column in laravel migration. Upon executing the query it does create column in the table but checking in postgresql for created enum types, it shows theres none. Does anyone ever experience this?

I am using laravel 5.4, php 7 and vagrant

Migration code

public function up()
    {
        Schema::create('restaurant_tables', function(Blueprint $table){
            $table->increments('_id');
            $table->string('tableNo', 100);
            $table->json('spatialLocation');
            $table->enum('tableStatus' , array('Occupied', 'Reserved', 'Vacant', 'For Cleaning'))->default('Vacant');
            $table->integer('numberOfCustomers');
            $table->integer('seatLimit');
            $table->string('tableDimension', 100);
            $table->enum('tableType', ['4x4','16x4','8x4']);
            $table->bigInteger('chairID');
        });

        Schema::table('restaurant_tables', function(Blueprint $table){
            $table->foreign('chairID')->references('_id')->on('restaurant_chairs');
        });
    }

Image in checking if enum data type created, and check if all tables are created

3
  • Does this answer your question? Laravel 5.3 Schema::create ENUM field is VARCHAR Commented Nov 18, 2019 at 3:51
  • @miken32 how can I implement it? the answer is pretty straightforward, would you like to explain a little bit more about it? the link you provide is related to my problem but the OP does not explain how he arrive to the answer Commented Nov 18, 2019 at 4:16
  • No idea; if Laravel/Doctrine won't do it you can just use raw SQL query in your migration. Commented Nov 18, 2019 at 4:21

3 Answers 3

4

You can simply do:

$table -> enum('tableStatus',['VACANT','OCCUPIED','RESERVED','FOR CLEANING'])->default('VACANT');
Sign up to request clarification or add additional context in comments.

1 Comment

This won't create real ENUM type. It will just add CHECK constraint like this: alter table "ads" add column "status" varchar(255) check ("status" in ('draft', 'in_moderation', 'published', 'declined', 'blocked', 'expired', 'closed', 'sold', 'paused')). And this is totally not you usually want from real PosgreSQL ENUM type which is stored as int value, not varchar and has performance of int type instead of varchar.
2
$table->enum('tableStatus',['Vacant', 'Reserved', 'Occupied', 'For Cleaning']);

First one will be default

Comments

1

Here is explanation: https://hbgl.dev/add-columns-with-custom-types-in-laravel-migrations/

Hope this will save some time for those who meets this problem like me.

In short:

Grammar::macro('typeTableTypeEnum', function () {
    return 'tableTypeEnum';
});
 
DB::unprepared("CREATE TYPE tableTypeEnum AS ENUM ('4x4','16x4','8x4');");
 
Schema::create('restaurant_tables', function (Blueprint $table) {
    ...
    $table->rawColumn('tableType', 'tableTypeEnum');
    ...
});

Alternative version (use for existing table updates):

DB::unprepared("CREATE TYPE \"tableTypeEnum\" AS ENUM ('4x4','16x4','8x4');");
DB::unprepared('ALTER TABLE "restaurant_tables" add column "tableType" "tableTypeEnum" NOT NULL');

Additional steps to extend Doctrine DBAL (required for renaming, for example):

  1. Create class TableTypeEnumDbType:
<?php

declare(strict_types=1);

namespace App\Models\Types;

use Doctrine\DBAL\Types\StringType;

class TableTypeEnumDbType extends StringType
{
    public function getName(): string
    {
        return 'tableTypeEnum';
    }
}
  1. add this to config/database.php:
/*
|--------------------------------------------------------------------------
| Doctrine DBAL settings.
|--------------------------------------------------------------------------
*/
'dbal' => [
    'types' => [
        'tableTypeEnum' => \App\Models\Types\AdStatusType::class,
    ],
],

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.