I have a Laravel application. I am performing the user information update function, but I can’t bypass the validation check for the admin_email and admin_title fields in the tbl_admin database. I am using the following FormRequest:
<?php
namespace AppHttpRequests;
use AppModelsAdmin;
use IlluminateFoundationHttpFormRequest;
use IlluminateContractsValidationValidator;
use IlluminateValidationValidationException;
use IlluminateValidationRule;
class UserRequest extends FormRequest
{
/**
* 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()
{
$admin_id = $this->route('admin.edit');
return [
'account_title_name' => ['required', 'email', Rule::unique('tbl_admin', 'admin_email')->ignore($admin_id)],
'account_titleshow_name' => 'required|max:40',
'account_password_name' => 'required|min:8|regex:/^(?=.*[A-Z])(?=.*[!@#$%^&*()]).*$/',
'account_tell_name' => 'required|regex:/(0)[0-9]/|not_regex:/[a-z]/|min:10|max:10|unique:tbl_admin,admin_phone',
'account_roles_name' => 'required',
];
}
public function messages(){
return [
'account_title_name.required'=>':attribute là bắt buộc',
'account_title_name.email'=>':attribute không đúng định dạng email (Chứa @gmail.com)',
'account_title_name.max'=>':attribute vượt quá quy định :max',
'account_title_name.max'=>':attribute vượt quá quy định :max',
'account_title_name.unique'=>':attribute đã tồn tại',
'account_titleshow_name.required'=>':attribute là bắt buộc',
'account_titleshow_name.max'=>':attribute vượt quá quy định :max',
'account_password_name.required' => ':attribute là bắt buộc',
'account_password_name.min' => ':attribute có ít nhất :min ký tự',
'account_password_name.regex' => ':attribute phải chứa ký tự viết hoa và kí tự đặc biệt',
'account_tell_name.required'=>':attribute là bắt buộc',
'account_tell_name.min'=>':attribute phải đủ :min số',
'account_tell_name.max'=>':attribute vượt quá quy định :max số',
'account_tell_name.regex'=>':attribute không đúng định dạng',
'account_tell_name.not_regex'=>':attribute không đúng định dạng',
'account_tell_name.unique'=>':attribute đã tồn tại',
'account_roles_name.required' => ':attribute',
];
}
public function attributes(){
return [
'account_title_name'=>'Tên tài khoản',
'account_titleshow_name'=>'Tên hiển thị',
'account_password_name'=>'Mật khẩu',
'account_roles_name' => 'Chọn một quyền cho tài khoản',
'account_tell_name' => 'Số điện thoại',
];
}
protected function failedValidation(Validator $validator)
{
if($this->wantsJson())
{
$response = response()->json([
'status' => 0,
'error'=>$validator->errors()->toArray()//$validator->errors()
]);
}else{
$response = redirect()
//->route('guest.login')
->with('message', 'Ops! Some errors occurred')
->withErrors($validator);
}
throw (new ValidationException($validator, $response))
->errorBag($this->errorBag)
->redirectTo($this->getRedirectUrl());
}
}
I hope someone will help me fix this issue so that I can update the user information and save the data. Here is the code of my FormRequest.
1. Controller
public function Edit_user($admin_id)
{
$this->AuthLogin ();
$admin = Auth::user();
// Kiểm tra người dùng có phải là admin hay không
if($admin->admin_id) {
$edit_field = Admin::where('admin_id', $admin_id)->first();
$roles_show = AdminRole::where('admin_admin_id', $admin_id)->first();
}
return view('admin.users.form.edit_users', compact('roles_show','edit_field'));
}
public function Update_user(UserRequest $request, $admin_id)
{
$form_update = [
'admin_email' => $request->input('account_title_name'),
'admin_password' => md5($request->input('account_password_name')),
'admin_name' => $request->input('account_titleshow_name'),
'admin_phone' => $request->input('account_tell_name'),
//'admin_id' => $request->input('admin_id_name'),
];
$admin = Admin::whereid($admin_id )->update($form_update);
AdminRole::updateOrCreate(
['admin_admin_id' => $admin->admin_id],
['roles_id_roles' => $request->input('account_roles_name')]
);
Toastr::success('Cập nhật thành công', 'Thành công');
return response()->json(['status'=>1, 'msg'=>'Cập nhật thông tin người dùng thành công']);
}
2. Models Admin (Database tbl_admin)
<?php
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
use IlluminateFoundationAuthUser as Authenticatable;
use IlluminateNotificationsNotifiable;
class Admin extends Authenticatable
{
use HasFactory;
protected $fillable = [
'admin_id','admin_email', 'admin_password', 'admin_name','admin_phone'
];
protected $primaryKey = 'admin_id';
protected $table = 'tbl_admin';
public function roles(){
return $this->belongsToMany('AppModelsRoles');
}
public function getAuthPassword(){
return $this->admin_password;
}
public function hasAnyRoles($roles){
if(is_array($roles)){
foreach($roles as $role){
if($this->hasRole($role)){
return true;
}
}
}else{
if($this->hasRole($roles)){
return true;
}
}
return false;
}
public function hasRole($role){
if($this->roles()->where('name',$role)->first()){
return true;
}
return false;
}
}
3. Routes
route::get('/edit-user/{admin_id}','AppHttpControllersUserController@Edit_user')->name('admin.edit');
route::post('/update-user/{admin_id}','AppHttpControllersUserController@Update_user');``
4
Answers
My issue has not been resolved and this error appears in the Laravel log file
[previous exception] [object] (PDOException(code: 42S22): SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' at C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateDatabaseConnection.php:368) [stacktrace] #0 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateDatabaseConnection.php(368): PDO->prepare('select count()...') #1 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateDatabaseConnection.php(705): IlluminateDatabaseConnection->IlluminateDatabase{closure}('select count()...', Array) #2 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateDatabaseConnection.php(672): IlluminateDatabaseConnection->runQueryCallback('select count()...', Array, Object(Closure)) #3 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateDatabaseConnection.php(376): IlluminateDatabaseConnection->run('select count()...', Array, Object(Closure)) #4 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateDatabaseQueryBuilder.php(2414): IlluminateDatabaseConnection->select('select count(*)...', Array, false) #5 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateDatabaseQueryBuilder.php(2402): IlluminateDatabaseQueryBuilder->runSelect() #6 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateDatabaseQueryBuilder.php(2936): IlluminateDatabaseQueryBuilder->IlluminateDatabaseQuery{closure}() #7 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateDatabaseQueryBuilder.php(2403): IlluminateDatabaseQueryBuilder->onceWithColumns(Array, Object(Closure)) #8 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateDatabaseQueryBuilder.php(2863): IlluminateDatabaseQueryBuilder->get(Array) #9 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateDatabaseQueryBuilder.php(2791): IlluminateDatabaseQueryBuilder->aggregate('count', Array) #10 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateValidationDatabasePresenceVerifier.php(55): IlluminateDatabaseQueryBuilder->count() #11 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateValidationConcernsValidatesAttributes.php(856): IlluminateValidationDatabasePresenceVerifier->getCount('tbl_admin', 'admin_email', '[email protected]...', '59', 'id', Array) #12 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateValidationValidator.php(610): IlluminateValidationValidator->validateUnique('account_title_n...', '[email protected]...', Array, Object(IlluminateValidationValidator)) #13 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateValidationValidator.php(416): IlluminateValidationValidator->validateAttribute('account_title_n...', 'Unique') #14 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateValidationValidator.php(447): IlluminateValidationValidator->passes() #15 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateValidationValidatesWhenResolvedTrait.php(25): IlluminateValidationValidator->fails() #16 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateFoundationProvidersFormRequestServiceProvider.php(30): IlluminateFoundationHttpFormRequest->validateResolved() #17 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateContainerContainer.php(1265): IlluminateFoundationProvidersFormRequestServiceProvider->IlluminateFoundationProviders{closure}(Object(AppHttpRequestsUserRequest), Object(IlluminateFoundationApplication)) #18 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateContainerContainer.php(1230): IlluminateContainerContainer->fireCallbackArray(Object(AppHttpRequestsUserRequest), Array) #19 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateContainerContainer.php(1215): IlluminateContainerContainer->fireAfterResolvingCallbacks('App\Http\Reques...', Object(AppHttpRequestsUserRequest)) #20 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateContainerContainer.php(778): IlluminateContainerContainer->fireResolvingCallbacks('App\Http\Reques...', Object(AppHttpRequestsUserRequest)) #21 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateFoundationApplication.php(851): IlluminateContainerContainer->resolve('App\Http\Reques...', Array, true) #22 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateContainerContainer.php(694): IlluminateFoundationApplication->resolve('App\Http\Reques...', Array) #23 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateFoundationApplication.php(836): IlluminateContainerContainer->make('App\Http\Reques...', Array) #24 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateRoutingRouteDependencyResolverTrait.php(79): IlluminateFoundationApplication->make('App\Http\Reques...') #25 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateRoutingRouteDependencyResolverTrait.php(48): IlluminateRoutingControllerDispatcher->transformDependency(Object(ReflectionParameter), Array, Object(stdClass)) #26 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateRoutingRouteDependencyResolverTrait.php(28): IlluminateRoutingControllerDispatcher->resolveMethodDependencies(Array, Object(ReflectionMethod)) #27 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateRoutingControllerDispatcher.php(41): IlluminateRoutingControllerDispatcher->resolveClassMethodDependencies(Array, Object(AppHttpControllersUserController), 'Update_user') #28 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateRoutingRoute.php(262): IlluminateRoutingControllerDispatcher->dispatch(Object(IlluminateRoutingRoute), Object(AppHttpControllersUserController), 'Update_user') #29 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateRoutingRoute.php(205): IlluminateRoutingRoute->runController() #30 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateRoutingRouter.php(721): IlluminateRoutingRoute->run() #31 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminatePipelinePipeline.php(128): IlluminateRoutingRouter->IlluminateRouting{closure}(Object(IlluminateHttpRequest)) #32 C:xampphtdocsbao-hanh-demoappHttpMiddlewareAccessAdmin.php(23): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest)) #33 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminatePipelinePipeline.php(167): AppHttpMiddlewareAccessAdmin->handle(Object(IlluminateHttpRequest), Object(Closure)) #34 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateRoutingMiddlewareSubstituteBindings.php(50): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest)) #35 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminatePipelinePipeline.php(167): IlluminateRoutingMiddlewareSubstituteBindings->handle(Object(IlluminateHttpRequest), Object(Closure)) #36 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateFoundationHttpMiddlewareVerifyCsrfToken.php(78): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest)) #37 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminatePipelinePipeline.php(167): IlluminateFoundationHttpMiddlewareVerifyCsrfToken->handle(Object(IlluminateHttpRequest), Object(Closure)) #38 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateViewMiddlewareShareErrorsFromSession.php(49): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest)) #39 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminatePipelinePipeline.php(167): IlluminateViewMiddlewareShareErrorsFromSession->handle(Object(IlluminateHttpRequest), Object(Closure)) #40 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateSessionMiddlewareStartSession.php(121): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest)) #41 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateSessionMiddlewareStartSession.php(64): IlluminateSessionMiddlewareStartSession->handleStatefulRequest(Object(IlluminateHttpRequest), Object(IlluminateSessionStore), Object(Closure)) #42 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminatePipelinePipeline.php(167): IlluminateSessionMiddlewareStartSession->handle(Object(IlluminateHttpRequest), Object(Closure)) #43 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateCookieMiddlewareAddQueuedCookiesToResponse.php(37): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest)) #44 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminatePipelinePipeline.php(167): IlluminateCookieMiddlewareAddQueuedCookiesToResponse->handle(Object(IlluminateHttpRequest), Object(Closure)) #45 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateCookieMiddlewareEncryptCookies.php(67): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest)) #46 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminatePipelinePipeline.php(167): IlluminateCookieMiddlewareEncryptCookies->handle(Object(IlluminateHttpRequest), Object(Closure)) #47 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminatePipelinePipeline.php(103): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest)) #48 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateRoutingRouter.php(723): IlluminatePipelinePipeline->then(Object(Closure)) #49 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateRoutingRouter.php(698): IlluminateRoutingRouter->runRouteWithinStack(Object(IlluminateRoutingRoute), Object(IlluminateHttpRequest)) #50 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateRoutingRouter.php(662): IlluminateRoutingRouter->runRoute(Object(IlluminateHttpRequest), Object(IlluminateRoutingRoute)) #51 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateRoutingRouter.php(651): IlluminateRoutingRouter->dispatchToRoute(Object(IlluminateHttpRequest)) #52 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateFoundationHttpKernel.php(167): IlluminateRoutingRouter->dispatch(Object(IlluminateHttpRequest)) #53 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminatePipelinePipeline.php(128): IlluminateFoundationHttpKernel->IlluminateFoundationHttp{closure}(Object(IlluminateHttpRequest)) #54 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateFoundationHttpMiddlewareTransformsRequest.php(21): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest)) #55 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateFoundationHttpMiddlewareConvertEmptyStringsToNull.php(31): IlluminateFoundationHttpMiddlewareTransformsRequest->handle(Object(IlluminateHttpRequest), Object(Closure)) #56 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminatePipelinePipeline.php(167): IlluminateFoundationHttpMiddlewareConvertEmptyStringsToNull->handle(Object(IlluminateHttpRequest), Object(Closure)) #57 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateFoundationHttpMiddlewareTransformsRequest.php(21): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest)) #58 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateFoundationHttpMiddlewareTrimStrings.php(40): IlluminateFoundationHttpMiddlewareTransformsRequest->handle(Object(IlluminateHttpRequest), Object(Closure)) #59 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminatePipelinePipeline.php(167): IlluminateFoundationHttpMiddlewareTrimStrings->handle(Object(IlluminateHttpRequest), Object(Closure)) #60 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateFoundationHttpMiddlewareValidatePostSize.php(27): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest)) #61 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminatePipelinePipeline.php(167): IlluminateFoundationHttpMiddlewareValidatePostSize->handle(Object(IlluminateHttpRequest), Object(Closure)) #62 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateFoundationHttpMiddlewarePreventRequestsDuringMaintenance.php(86): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest)) #63 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminatePipelinePipeline.php(167): IlluminateFoundationHttpMiddlewarePreventRequestsDuringMaintenance->handle(Object(IlluminateHttpRequest), Object(Closure)) #64 C:xampphtdocsbao-hanh-demovendorfruitcakelaravel-corssrcHandleCors.php(38): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest)) #65 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminatePipelinePipeline.php(167): FruitcakeCorsHandleCors->handle(Object(IlluminateHttpRequest), Object(Closure)) #66 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateHttpMiddlewareTrustProxies.php(39): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest)) #67 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminatePipelinePipeline.php(167): IlluminateHttpMiddlewareTrustProxies->handle(Object(IlluminateHttpRequest), Object(Closure)) #68 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminatePipelinePipeline.php(103): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest)) #69 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateFoundationHttpKernel.php(142): IlluminatePipelinePipeline->then(Object(Closure)) #70 C:xampphtdocsbao-hanh-demovendorlaravelframeworksrcIlluminateFoundationHttpKernel.php(111): IlluminateFoundationHttpKernel->sendRequestThroughRouter(Object(IlluminateHttpRequest)) #71 C:xampphtdocsbao-hanh-demoindex.php(52): IlluminateFoundationHttpKernel->handle(Object(IlluminateHttpRequest)) #72 {main} "}
Change these
To this
you can access the parameters in the Update_User function by using the $this keyword.
then the rule() function is like this:
$this keyword also applies to fetch other fields, like
$this->account_title_name
,$this->account_titleshow_name
.…..
if you get error then you have to tell what the primary_key column.
then it can be solved like this:
let me give details:
unique:table_name,column_name,except_based_id,primary_key
I followed the instructions as directed, but still failed and Laravel log reported an error (Column not found: 1054 Unknown column ‘id’ in ‘tbl_admin’). Currently, I am using ‘admin_id’ as the primary key in the ‘tbl_admin’ table. I would appreciate any help with this issue.