char fields in a migration laravel

0

I am creating a migration in Laravel, and I have a field defined like this:

$table->char('código', 3);

The code data should be made up of three digits, from 000 to 999 , but I can not use a numeric field, because the leading zeros should also appear. So, I have to use a field char , but add some kind of restriction, so that I only accept values formed by, exactly, three digits.

The question is whether this can be specified in any way in the migration, or in the model, or by programming in the controllers that affect this field. I am using the MySQL driver. As the migration refers only to the structure of the table, I do not see very clearly that this is the right place for this restriction.

Has anyone had to do this before?

Thank you.

    
asked by Chefito 30.11.2018 в 23:46
source

1 answer

1

There is no parameter that allows you to specify exactly three characters, however, if a user enters these codes and would like to force them to enter three characters, simply validate the length of the field of at least three characters in your controller :

$this->validate($request, [
    "código" : "min:3"
]);
    
answered by 01.12.2018 / 00:23
source