验证更新时遇到问题 (Laravel 5)

Having trouble validating on update (Laravel 5)

这是我在用户控制器中的更新功能

public function update_user_credentials(UpdateUserRequest $request)
{ 
    $user = User::find($request->user()->id);
    if(!$user) 
    {
        return response('User not found', 404);
    }

    try
    {
        $data=Input::all();
        $user->fill($data);
        var_dump($user);
        exit;
        $user->save();

    } 
        catch(Exception $ex)
    {
        return response($ex->getMessage(),400);
        echo Success::get('message');
    } 

    return Redirect::back()->with('message','updated');   
}

我的UpdateUserRequest.php

<?php
namespace App\Http\Requests;

use App\Http\Requests\Request;

class UpdateUserRequest extends Request
{
    /**
     * 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 [
                'first_name' =>'required',
                'last_name'=>'required',
                'url'=>'url',
                'password'=>'min:6|confirmed',
                'password_confirmation'=>'min:6',
                'email'=>'email|unique:users,email',            
                ];
            }

}

每一列都有自己的表格。所以更新电子邮件和密码一样有自己的形式。

当我在没有将 UpdateUserRequest 放入我的更新控制器的情况下进行更新时,它工作正常。但是当我将其添加到验证中时,什么也没有发生。我收到 302 错误,但没有收到任何消息。

我尝试使用 validator->messages 获取消息,但还是一无所获。

如果我输入

protected $redirect = '/'

我确实被重定向了。这意味着验证工作正常?

如果有帮助,这是我的路线:

Route::get('/account/email',function(){
    $user=Request::user();
    $id = $user->id;
    return Response::view('user.edit.email', compact('user'));
});

Route::patch('/account/update','UserController@update_user_info');

更新 所以我想出了如何显示错误消息,其中之一就是 'first name and last name were required'。但是现在我尝试更新

时出现此错误
ErrorException in UserController.php line 93:
Missing argument 2 for App\Http\Controllers\UserController::update_user_credentials()
in UserController.php line 93
at HandleExceptions->handleError('2', 'Missing argument 2 for App\Http\Controllers\UserController::update_user_credentials()', '/Users/Jack/projects/makersBrand/laravel/app/Http/Controllers/UserController.php', '93', array('request' => object(UpdateUserRequest))) in UserController.php line 93
at UserController->update_user_credentials(object(UpdateUserRequest))
at call_user_func_array(array(object(UserController), 'update_user_credentials'), array(object(UpdateUserRequest))) in Controller.php line 256
at Controller->callAction('update_user_credentials', array(object(UpdateUserRequest))) in ControllerDispatcher.php line 164
at ControllerDispatcher->call(object(UserController), object(Route), 'update_user_credentials') in ControllerDispatcher.php line 112
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 114
at ControllerDispatcher->callWithinStack(object(UserController), object(Route), object(Request), 'update_user_credentials') in ControllerDispatcher.php line 69
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\UserController', 'update_user_credentials') in Route.php line 201
at Route->runWithCustomDispatcher(object(Request)) in Route.php line 134
at Route->run(object(Request)) in Router.php line 704
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Router.php line 706
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 671
at Router->dispatchToRoute(object(Request)) in Router.php line 631
at Router->dispatch(object(Request)) in Kernel.php line 236
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in VerifyCsrfToken.php line 50
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 62
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 122
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 87
at Kernel->handle(object(Request)) in index.php line 54

这是否意味着“2”被传递到验证中?

我认为您的验证规则不起作用。如果您更新数据,您的验证规则也会检查您所在的同一行 updating.What 我想说的是,假设您正在尝试更新具有 id 2 的行,那么当您进行验证时,您的验证规则也会检查具有 id 2 的行的唯一性 email.Suppose 你没有更新你的电子邮件,然后在验证你的验证规则时将检查所有行并且它不会找到唯一的电子邮件并且它应该显示错误。

你的路线必须是

Route::post('/account/update/{id}','UserController@update_user_info');

虽然更新您的规则必须检查除您所在的行之外的所有行 updating.You 可以应用相同的规则来创建和更新这样的数据

public function rules(){
return [
     'first_name' =>'required',
     'last_name'=>'required',
     'url'=>'url',
     'password'=>'min:6|confirmed',
     'password_confirmation'=>'min:6',
     // you need to change here 

     'email'=>'email|unique:users,email,'.$this->route()->getParameter('id').',database_id'            
];
// database_id means your table primary key column i.e id

}

我想你的更新函数一定是这样的

public function update_user_credentials(Requests\UpdateUserRequest $request,$id){
// code
}

在 L5.3+ 中处理这个问题的一种流畅方法是使用 Rule facade

use Illuminate\Validation\Rule;

Validator::make($data, [
    'email' => [
    'required',
    Rule::unique('users')->ignore($user->id),
  ],
]);

请注意,您需要将验证规则指定为数组,而不是使用 |分隔规则的字符。