How to put the zerofill Constraint in Laravel 5.6?

Asked

Viewed 71 times

2

Good afternoon. I’m having a problem making use of the zerofill Constraint in Laravel because I saw that it is not in the documentation.

I have a field in my table that is enrollment and in my view I am limiting this field to 5 digits, so the maximum value that it can reach is: 99999. But I would like when the user typed a registration number without using all the digits, the spaces were filled with 0.

For example, the user informs the registration number: 450. The bank should be showing 00450.

How can I do it in the Laravel?

My Migration is like this:

$table->integer('matricula')->unique();

1 answer

0


You can use the Mutators or Accessors to do this. The Mutatosr act as a SET, before saving to the bank, it would check the number of digits and add the zeros at the beginning. o Accessors would be like the GET, when taking the value of the bank, it would check the number of digits and add in front of the value, but only for display.

You can see examples in the link:

https://code.tutsplus.com/tutorials/eloquent-mutators-and-accessors-in-laravel--cms-30312

Browser other questions tagged

You are not signed in. Login or sign up in order to post.