在 Laravel 中指定存在验证规则时,是否可以引用另一个字段?我想说输入a必须存在于表a中,输入b必须存在于表b中并且表b中列x的值必须等于输入a。
最好用例子来解释:
public $rules = array(
'game_id' => 'required|exists:games,id',
'team1_id' => 'required|exists:teams,id,game_id,<game_id input value here>',
'team2_id' => 'required|exists:teams,id,game_id,<game_id input value here>'
);
因此,通过我的验证规则,我希望能够确保:
game_id
存在于games
表中(id
字段)team1_id
exists within theteams
table (id
field) and thegame_id
column (in theteams
table)必须等于game_id
输入的值。- 如上
team2_id
所以,如果在我的表格中,我输入 1
为 game_id
,我希望能够确保 team1_id
和 team2_id
具有值 1
为 game_id
。
我希望这是有道理的。
谢谢
原文由 Jonathon 发布,翻译遵循 CC BY-SA 4.0 许可协议
你想要一个 自定义验证规则,我会为此创建一个单独的类。但为简洁起见,这里使用内联闭包几乎相同:
然后简单地使用这个: