dongqu1783 2016-04-04 21:19 采纳率: 100%
浏览 825
已采纳

Laravel验证:如何检查给定数组中是否存在值?

我尝试了很多验证文档中的方法,但都给出了同样的错误说明:

Array to string conversion

下面是我如何添加数组的:

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

对于如何实现这一点有什么提示吗?

我创建了一个自定义验证器,但仍然无法传递数组。

  • 写回答

2条回答 默认 最新

  • duai5344 2016-04-04 21:37
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?