在Laravel中指定存在驗證規則時是否有引用另一個字段的方法?我想能夠說輸入a必須存在於表a中,輸入b必須存在於表b中,並且表b中列x的值必須等於輸入a。Laravel驗證:與附加列條件存在 - 自定義驗證規則
最好的例子來說明:
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
存在於teams
表(id
字段)和game_id
列(在teams
表)必須等於game_id
輸入的值。- 如上所述,對於
team2_id
所以,如果在我的形式,我進入1
爲game_id
,我希望能夠確保兩者team1_id
和team2_id
球隊表中的記錄具有價值1
game_id
。
我希望這是有道理的。
感謝
我一直在使用這種格式,因爲5.3,它爲我工作。我很確定它是有效的,儘管我沒有在他們的文檔中看到它。 $ this-> mymodel-> create_rules ['company_id'] ='required | exists:companies,id,type_id,'。 $ ID; 這意味着company_id必須存在於其自己的表中,並且type_id字段必須是$ id的值。 – 2017-07-11 13:27:27