1

i have a migration with the following:

Schema::create('ingredients', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name',10)->unique()->nullable(false);
    $table->timestamps();
});

If you note i want the string not to exceed 10 characters

In my controller i have:

$this->validate($request, [
    'name' => 'bail|required|unique:ingredients|max:200',
]);

In the controller i have mentioned the max characters to be 200.

So when i fill the form with more than 10 characters and less than 200 characters it get stored in the database. How is this possible.

Will laravel not check the integrity between then migration files schema rules and the validation rules.

Also another thing i noted is: If i remove the validation rules from the controller and submit the form, still it does not check for the number of character w.r.t 10. It creates a new database record. Where as it checks for the unique record even i dont use any validation rules.

why unique is checked is checked irrespective of mentioning in the validation rules.

Also the maximum number of characted (10) in the migration file has no significance. because with or without validation rules i can input name with more than 10 characters.

Where as if i have my validation rule saying max:2 then it shows the form errors while submitting.

So its bit confusing which work from migration file irrespective of the validation rules and which does not work at all even its mentioned in the migration table.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
Santhosh
  • 9,965
  • 20
  • 103
  • 243

2 Answers2

0

This is how it works. Database is separate from PHP. Normally assuming you don't work with really big data, you create for example 100 characters column in your migration:

$table->string('name',100)->unique()->nullable(false);

and in your validation you use same limit or less (for example 20 characters). This way in case you want to raise limit to 50 characters you can do it only in application without touching database but keep in mind this is not the best solution when you expect a lot of data - in such case you should rather use the smallest sizes you need.

Laravel won't check column max size in database for you. You could try to add such helper for yourself but probably it doesn't make much sense to do it.

When you develop your application you have to know what is going on. For example Laravel also won't magically verify if you have column in table. You need to add it in migration and if you don't do it, when you try to insert data, you will get exception.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
0

i have two conditions string('name',10) and the max:200

I am using sqlite. Its doesn't check with the number of characters.

Note that numeric arguments in parentheses that following the type name (ex: "VARCHAR(255)") are ignored by SQLite - SQLite does not impose any length restrictions (other than the large global SQLITE_MAX_LENGTH limit) on the length of strings, BLOBs or numeric values.

Thats why the number of characters are not being accounted.

If i dont mention unique in the validation, then i cant show it in the form. Else it will display a laravel database error.

Santhosh
  • 9,965
  • 20
  • 103
  • 243