16

So, okay i tried a lot of rules from validation docs but all give me same error saying

Array to string conversion

Here is how I add the array:

$this->validate($request,[
                'employee' => 'required|in:'.$employee->pluck('id')->toArray(),
            ],[
                'employee.in' => 'employee does not exists',
            ]);

Any hint on how to achieve this?

i created a custom validator but still passing array seems to be not possible

0

3 Answers 3

34

You are now able to use the Rule class instead of imploding values yourself as described in the correct answer. Simply do:

$ids = $employee->pluck('id')->toArray();
['employee' => ['required', Rule::in($ids)]];

As mentioned in the documentation for the "in" rule.

Sign up to request clarification or add additional context in comments.

1 Comment

This is a much cleaner approach. Here the link to its documentation in laravel 8.
33

Implode the array as a string and join it on commas.

'employee' => 'required|in:'.$employee->implode('id', ', '),

This will make the correct comma separated string that the validator expects when making an in comparison.

Edit

This still works, but is not the Laravelesque way of doing it anymore. See the answer by @nielsiano.

Comments

0

This can be accomplished in for ways

  1. You can do like this you need to user use Illuminate\Validation\Rule class top of your script

    $request->validate ([
         'someProperty' => [
             'required',
              Rule::in(['manager', 'delivery_boy', 'stuff'])
         ]
    ]);
    
  2. You can also do like this in: required a string like 'required|in:manager, delivery_boy, stuff'

    "required|in:" . implode(", ",$rules)

    $rules = ['manager', 'delivery_boy', 'stuff'];
    $request->validate([
        'someProperty' => "required|in:" . implode(", ",$rules)
    ]);
    
  3. This way will work when you're using Validator class

    \Validator::make($request->all(),[
            'someProperty' => [
                'required',
                Rule::in(['manager', 'delivery_boy', 'stuff'])
            ]
        ])
    
  4. When you use Laravel form request class for validation request

    public function rules(): array
    {
        return [
            'someProperty' => [
                'required',
                Rule::in(['manager', 'delivery_boy', 'stuff'])
            ]
        ];
    }
    
    
    public function authorize(): bool
    {
        return true;
    }
    

3 Comments

Is this really just a copy of the other two answers into a single post? Do researchers need to see the same solutions ...again?
this is not copy paste I think this will help for developer to know multiple way to implement
But you are just re-providing the same techniques that were already provided by the 2 earlier answers, right?

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.