这是我在usercontroller中的更新功能
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-&gt;消息获取消息,但也没有得到任何消息。
如果我放入
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');
更新所以我想出了如何显示错误消息,其中一个是“第一个名字和姓氏是必需的&#39;”。但是现在我在尝试更新
时收到此错误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
这是否意味着&#39; 2&#39;正在通过验证?
答案 0 :(得分:0)
我认为您的验证规则不起作用。如果您更新数据,验证规则也会检查您正在更新的同一行。我想说的是假设您正在尝试更新ID为2的行,那么当您进行验证时,您的验证规则也会检查对于email的唯一性,ID为2的行。假设您没有更新您的电子邮件,那么验证验证规则时会检查所有行,但它不会找到唯一的电子邮件,并且应该显示错误。
您的路线必须
Route::post('/account/update/{id}','UserController@update_user_info');
更新规则时必须检查除了要更新的行之外的所有行。您可以应用相同的规则来创建和更新这样的数据
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
}
答案 1 :(得分:0)
在L5.3 +中处理此问题的流畅方法是使用规则外观
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
]);
请注意,您需要将验证规则指定为数组,而不是使用|用来划定规则的字符。