且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-11-01 10:12:32

将数组放大为字符串并以逗号连接.

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

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

这将生成验证器在进行in比较时期望的正确的逗号分隔的字符串.

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

修改

这仍然有效,但已不再是 Laravelesque 的使用方式.请参阅@nielsiano的答案.

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