Monday, February 27, 2017

How to add a constraint to a table with Laravel's migration?

Let's say I have a table periods with columns start_date and end_date and I want to add a constraint start_date < end_date.

The PostgreSQL query would be:

ALTER TABLE periods ADD CONSTRAINT check_dates CHECK ("start_date" < "end_date");

But I want to do it as a migration with PHP. I guess it will look something like this:

Schema::table('periods', function (Blueprint $table) { $table->something(); });

...but even with IntelliSense I couldn't guess what to write instead of "something()".

I'm not using the newest version of Laravel so it would be nice if you add from which version your code works and what can you do in older versions.




via user1

Advertisement