Laravel Validation - How to check if a value exists in a given array?

前端 未结 2 456
谎友^
谎友^ 2021-02-14 18:38

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

2条回答
  •  悲&欢浪女
    2021-02-14 18:50

    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.

提交回复
热议问题