Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lukegalea16's avatar

Migration - Enter Columns before created_at and updated_at columns

Is there a way to always enter columns before the created_at column in migrations? The only option I know of is the after clause but then the name of the previous column in the DB has to be entered.

0 likes
8 replies
siangboon's avatar

i don't understand, can you explain further?

lukegalea16's avatar

Hi guys, the laravel command ->after('column') inserts a column after a specified one in a migration. I am looking for the opposite i.e. to enter a column previous to or before another column.

Sinnbeck's avatar

Just set it ->after('id') and it will be after id but before created_at. There isnt a ->before() to my knowledge.

Otherwise, you could just check what the last column is before the created at and use that?

lukegalea16's avatar

Well I already have other columns, which is why I asked if a 'before' exists. I could definitely do that yes but if a 'before' existed it would have been easier to always insert them before the created_at, not having to check what was previous to.

Anyways, just wanted to double-check. Cheers.

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Sadly I dont think mysql supports BEFORE in an alter statement, so that is most likely why it does not exist. It would need some "hacks" to first find the column before doing the migration then.

1 like
Tray2's avatar

If you still are in the development stage (not having put it to production) update the migrations instead of adding new ones.

Since there is no before option in SQL it's kinda tricky to acheive but since you should now the column before you should be able to use that.

From https://www.mysqltutorial.org/mysql-add-column/

Third, MySQL allows you to add the new column as the first column of the table by specifying the FIRST keyword. It also allows you to add the new column after an existing column using the AFTER existing_column clause. If you don’t explicitly specify the position of the new column, MySQL will add it as the last column.

1 like
lukegalea16's avatar

Yes and this - I'm still in development so I will most likely do this to keep my migrations 'clean'. Thanks :-)

Please or to participate in this conversation.