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

Marins's avatar

migration with leading ceros if number length smaller

Hello,

I have a column for project_number(this is not the id to the table). I need this number to show with leading 0 for a 4 digits length, like this:

1203

0003

0452

etc, how could achieve this with a migration, or somehow?

Thank you

0 likes
2 replies
Snapey's avatar

You don't say how you will create the project number?

Assuming project_number column is string in the database, then you could use a mutator to set it.

Alternatively you could let it rest in the database in numeric (non-padded) form and create an accessor so that whenever you read the number it was padded out.

https://laravel.com/docs/5.4/eloquent-mutators#accessors-and-mutators

Accessors and mutators are eloquent features so neither of the above would be of use if you plan to use DB directly

Please or to participate in this conversation.