Php Laravel 5.7请求验证不存在';I don’我没有按预期工作
我使用Laravel提交表格。以下是我的web.php路线:Php Laravel 5.7请求验证不存在';I don’我没有按预期工作,php,laravel,Php,Laravel,我使用Laravel提交表格。以下是我的web.php路线: Route::middleware(['auth'])->prefix('account')->namespace('Account')->group(function () { Route::get('informations', 'AccountController@index')->name('account.informations'); Route::post('information
Route::middleware(['auth'])->prefix('account')->namespace('Account')->group(function () {
Route::get('informations', 'AccountController@index')->name('account.informations');
Route::post('informations', 'AccountController@update')->name('account.informations.post');
});
我的控制器AccountController.php:
/**
* @param UpdateMember $request
* @return \Illuminate\Http\RedirectResponse
*/
public function update(UpdateUser $request)
{
dd($request->all());
$user = User::where('id', Auth::user()->id)
->update($request->all());
return redirect()->route('account.informations');
}
以及我的UpdateUser.php:
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'lastname' => 'required|string|max:255',
'firstname' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users|confirmed',
'password' => 'required|string|min:6|confirmed',
];
}
我的问题是:当我在控制器中使用UserUpdate$request时,我无法访问该函数,因此未显示dd($request->all())
但如果我替换:
public function update(UpdateUser $request)
借
我的控制器接通了。我做错了什么?由于您的验证规则在更新现有记录时返回了一封已存在的错误电子邮件,因此像电子邮件这样的唯一字段验证如何处理。这可以通过下面的示例来解决
public function rules()
{
$client = Client::find($this->client);
return [
'name' => 'required|string',
'address' => 'required|string',
'gender' => 'required|string',
'dob' => 'required|string',
'phone' => 'required|string',
'email' => 'required|unique:clients,email,' . $client->id,
'password' => 'regex:/^[0-9a-zA-Z]*$/',
'profile-photo' => 'image|mimes:jpg,png,jpeg',
'c_name' => 'nullable|regex:/^[a-zA-Z ]*$/',
'c_address' => 'nullable|regex:/^[a-zA-Z ]*$/',
'c_email' => 'nullable|email|unique:clients,company_email,'. $client->id,
'c_contact' => 'nullable|regex:/^[0-9]*$/',
];
}
在我的例子中,原因是我放弃在请求头中添加“Accept:application/json”。添加该标题后,一切正常,这意味着存在验证错误或授权错误。FormRequests在解析时即在调用控制器上的方法之前进行验证。验证错误存储在会话中,因此您将能够看到验证的哪一部分失败了是的,我忘了包含错误视图。。。我太笨了。谢谢你,这真是愚蠢@文森特·德科你能解释一下你是怎么解决的吗?我真的被卡住了。
public function rules()
{
$client = Client::find($this->client);
return [
'name' => 'required|string',
'address' => 'required|string',
'gender' => 'required|string',
'dob' => 'required|string',
'phone' => 'required|string',
'email' => 'required|unique:clients,email,' . $client->id,
'password' => 'regex:/^[0-9a-zA-Z]*$/',
'profile-photo' => 'image|mimes:jpg,png,jpeg',
'c_name' => 'nullable|regex:/^[a-zA-Z ]*$/',
'c_address' => 'nullable|regex:/^[a-zA-Z ]*$/',
'c_email' => 'nullable|email|unique:clients,company_email,'. $client->id,
'c_contact' => 'nullable|regex:/^[0-9]*$/',
];
}