0

I have a {{ Form::textarea('name') }} for add an array.

In a controller I use:

$input = $request->all();
$name = explode(PHP_EOL, $input['name']);

$this->validate($request, [
    'name' => Rule::unique('table1')->where(function ($query) {
        global $name;
        $query->whereIn('name', $name);
    })
]);

But it does not work. How to validation array for unique values?

Sorry for my english.

2
  • Doesn't work because name is a single string, not an array Commented May 20, 2017 at 23:04
  • Just a tip : if you want to use $name inside of the anonymous function, add use ($name) like this : function($query) use ($name){ // ... } Commented May 21, 2017 at 2:41

1 Answer 1

1

The easy approach. If you want better control and ability to use this with the validate method then i'd suggest creating a custom validation rule.

$data = [
    'name' => explode(PHP_EOL, $request->input('name'))
];

$validator = \Validator::make($data, [
    'name.*' => 'unique:table1,name',
]);

if ($validator->fails()) {
    // Handle failed logic
}
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry. Maybe you know. I use return redirect('...')->withErrors($validator) ->withInput(); for output errors. It output errors as The name.0 has already been taken. How to show the value instead of name.0.

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.