我想通过在Laravel中的请求中调用一个特殊函数来验证不同的变量。我该怎么办?
if ($this->function() == 'function1') {
return [
'firstName' => 'required|unique:networks,name|min:2',
];
} elseif ($this->function() == 'function2') {
return [
'lastName' => 'required|unique:networks,name|min:5'
];
}
我的路线:
Route::post('increase-credit/{value?}', 'testController@function1');
Route::post('increase-credit-by-admin/{value?}', 'testController@function2');
答案 0 :(得分:1)
您可以命名您的路线,然后在FormRequest中根据路线名称返回不同的规则:
Route::post('increase-credit/{value?}', 'testController@function1')->name('route1');
Route::post('increase-credit-by-admin/{value?}', 'testController@function2')->name('route2');
在FormRequest中:
if ($this->route()->getName() == 'route1') {
return [
rule1
];
if ($this->route()->getName() == 'route2') {
return [
rule2
];
}