Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer or String for a phone number?

I want to know if I have to use a string or an integer for a phone number?

I have tried an integer but I have a problem in my validation.

... table->integer('phone'); ...

In my validation, I must have between 8 and 11 characters.

I have tried this, but it doesn't work:

'phone' => 'required|numeric|between:8,11',

I think the string is better?

like image 739
user11124425 Avatar asked Aug 11 '19 09:08

user11124425


1 Answers

If you want to do some calculations with the numbers which you going to be storing (inserting), then you have to use int(in Laravel migration: it is integer) as the data type of the field. However If you do not want to do some calculations with it, then use the data type as varchar (in Laravel migration: it is string) as the data type of the field.

So, when it comes to storing Phone numbers, you can use the varchar data type as you do not have to do calculations with Phone numbers.

So in this case, your validation should be this:

'phone' => 'required|string|min:8|max:11'
like image 119
Kaz Avatar answered Oct 23 '22 15:10

Kaz