4

I have a form:

<input type="text" name="name[1]">
<input type="text" name="name[2]">
<input type="text" name="name[3]">
<input type="submit" value="Submit">

I've created form validation file with rules:

class formRequest extends FormRequest {
....
public function rules()
{
    return ['name.*' => 'unique:names'];
}

public function messages()
{
    return ['name.unique' => 'Name is already in DB!'];
}

After submitting the form with value (e.g. 'John') in input (e.g. name[1]), which already exists in the DB, I get:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name.1' in 'where clause' (SQL: select count(*) as aggregate from `names` where `name`.`1` = John)

So it's treating name.* as for creating another field name, instead of looping through the array.

My Laravel Framework is version 5.4.19. According to the docs, the name.* should work to iterate through an array during validation.

What I am doing wrong?

1

1 Answer 1

2

by default if you didn't provide a column name for the unique rule, Laravel will add the input name, and in your case the input name is a number.

to fix this issue change the rule to the following instead:

return ['name.*' => 'unique:names,name'];
Sign up to request clarification or add additional context in comments.

4 Comments

Hey, man! I've spent a day looking for this solution. Where did they write it that I should provide column name???:) This really makes learning programming so hard, because you just don't know where to look for the needed information and spend days trying this and that...
Do you have an advice on how to learn programming, or, particularly, Laravel?:)
Actually the documentation of Laravel is good when you faced a problem i suggest to start from there, and the best website to learn Laravel is Laracast but it is not free :( check unique validation here
thank you! I've read that page several times before posting this question, but didn't get to the end where they list 'Availbale Validation Rules"... Next time I'll read whole page:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.