我有一个Laravel应用程序。我正在执行用户信息更新功能,但我无法绕过tbl_admin数据库中admin_email和admin_title字段的验证检查。我正在使用以下FormRequest:
<?php
namespace App\Http\Requests;
use App\Models\Admin;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Validation\ValidationException;
use Illuminate\Validation\Rule;
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());
}
}
我希望有人能帮我解决这个问题,这样我就可以更新用户信息并保存数据。下面是我的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.模型管理(数据库tbl_admin)
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
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('App\Models\Roles');
}
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.路线
route::get('/edit-user/{admin_id}','App\Http\Controllers\UserController@Edit_user')->name('admin.edit');
route::post('/update-user/{admin_id}','App\Http\Controllers\UserController@Update_user');``
3条答案
按热度按时间3phpmpom1#
换掉这些
到这个
mwkjh3gx2#
您可以使用$this关键字访问Update_User函数中的参数。
那么rule()函数就像这样:
$this关键字也适用于获取其他字段,如
$this->account_title_name
、$this->account_titleshow_name
azpvetkf3#
我的问题尚未解决,此错误显示在Laravel日志文件中
[previous exception] [object] (PDOException(code: 42S22): SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' at C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Database\Connection.php:368) [stacktrace] #0 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Database\Connection.php(368): PDO->prepare('select count()...') #1 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Database\Connection.php(705): Illuminate\Database\Connection->Illuminate\Database{closure}('select count()...', Array) #2 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Database\Connection.php(672): Illuminate\Database\Connection->runQueryCallback('select count()...', Array, Object(Closure)) #3 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Database\Connection.php(376): Illuminate\Database\Connection->run('select count()...', Array, Object(Closure)) #4 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php(2414): Illuminate\Database\Connection->select('select count(*)...', Array, false) #5 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php(2402): Illuminate\Database\Query\Builder->runSelect() #6 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php(2936): Illuminate\Database\Query\Builder->Illuminate\Database\Query{closure}() #7 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php(2403): Illuminate\Database\Query\Builder->onceWithColumns(Array, Object(Closure)) #8 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php(2863): Illuminate\Database\Query\Builder->get(Array) #9 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php(2791): Illuminate\Database\Query\Builder->aggregate('count', Array) #10 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Validation\DatabasePresenceVerifier.php(55): Illuminate\Database\Query\Builder->count() #11 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Validation\Concerns\ValidatesAttributes.php(856): Illuminate\Validation\DatabasePresenceVerifier->getCount('tbl_admin', 'admin_email', 'bao123@gmail.co...', '59', 'id', Array) #12 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Validation\Validator.php(610): Illuminate\Validation\Validator->validateUnique('account_title_n...', 'bao123@gmail.co...', Array, Object(Illuminate\Validation\Validator)) #13 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Validation\Validator.php(416): Illuminate\Validation\Validator->validateAttribute('account_title_n...', 'Unique') #14 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Validation\Validator.php(447): Illuminate\Validation\Validator->passes() #15 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Validation\ValidatesWhenResolvedTrait.php(25): Illuminate\Validation\Validator->fails() #16 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Foundation\Providers\FormRequestServiceProvider.php(30): Illuminate\Foundation\Http\FormRequest->validateResolved() #17 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Container\Container.php(1265): Illuminate\Foundation\Providers\FormRequestServiceProvider->Illuminate\Foundation\Providers{closure}(Object(App\Http\Requests\UserRequest), Object(Illuminate\Foundation\Application)) #18 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Container\Container.php(1230): Illuminate\Container\Container->fireCallbackArray(Object(App\Http\Requests\UserRequest), Array) #19 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Container\Container.php(1215): Illuminate\Container\Container->fireAfterResolvingCallbacks('App\Http\Reques...', Object(App\Http\Requests\UserRequest)) #20 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Container\Container.php(778): Illuminate\Container\Container->fireResolvingCallbacks('App\Http\Reques...', Object(App\Http\Requests\UserRequest)) #21 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(851): Illuminate\Container\Container->resolve('App\Http\Reques...', Array, true) #22 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Container\Container.php(694): Illuminate\Foundation\Application->resolve('App\Http\Reques...', Array) #23 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(836): Illuminate\Container\Container->make('App\Http\Reques...', Array) #24 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Routing\RouteDependencyResolverTrait.php(79): Illuminate\Foundation\Application->make('App\Http\Reques...') #25 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Routing\RouteDependencyResolverTrait.php(48): Illuminate\Routing\ControllerDispatcher->transformDependency(Object(ReflectionParameter), Array, Object(stdClass)) #26 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Routing\RouteDependencyResolverTrait.php(28): Illuminate\Routing\ControllerDispatcher->resolveMethodDependencies(Array, Object(ReflectionMethod)) #27 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php(41): Illuminate\Routing\ControllerDispatcher->resolveClassMethodDependencies(Array, Object(App\Http\Controllers\UserController), 'Update_user') #28 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Routing\Route.php(262): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(App\Http\Controllers\UserController), 'Update_user') #29 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Routing\Route.php(205): Illuminate\Routing\Route->runController() #30 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Routing\Router.php(721): Illuminate\Routing\Route->run() #31 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(128): Illuminate\Routing\Router->Illuminate\Routing{closure}(Object(Illuminate\Http\Request)) #32 C:\xampp\htdocs\bao-hanh-demo\app\Http\Middleware\AccessAdmin.php(23): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Illuminate\Http\Request)) #33 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): App\Http\Middleware\AccessAdmin->handle(Object(Illuminate\Http\Request), Object(Closure)) #34 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Routing\Middleware\SubstituteBindings.php(50): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Illuminate\Http\Request)) #35 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Routing\Middleware\SubstituteBindings->handle(Object(Illuminate\Http\Request), Object(Closure)) #36 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken.php(78): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Illuminate\Http\Request)) #37 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure)) #38 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\View\Middleware\ShareErrorsFromSession.php(49): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Illuminate\Http\Request)) #39 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure)) #40 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Session\Middleware\StartSession.php(121): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Illuminate\Http\Request)) #41 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Session\Middleware\StartSession.php(64): Illuminate\Session\Middleware\StartSession->handleStatefulRequest(Object(Illuminate\Http\Request), Object(Illuminate\Session\Store), Object(Closure)) #42 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure)) #43 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse.php(37): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Illuminate\Http\Request)) #44 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure)) #45 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\EncryptCookies.php(67): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Illuminate\Http\Request)) #46 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure)) #47 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Illuminate\Http\Request)) #48 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Routing\Router.php(723): Illuminate\Pipeline\Pipeline->then(Object(Closure)) #49 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Routing\Router.php(698): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request)) #50 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Routing\Router.php(662): Illuminate\Routing\Router->runRoute(Object(Illuminate\Http\Request), Object(Illuminate\Routing\Route)) #51 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Routing\Router.php(651): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request)) #52 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(167): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request)) #53 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(128): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http{closure}(Object(Illuminate\Http\Request)) #54 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Illuminate\Http\Request)) #55 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull.php(31): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure)) #56 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull->handle(Object(Illuminate\Http\Request), Object(Closure)) #57 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Illuminate\Http\Request)) #58 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TrimStrings.php(40): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure)) #59 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\TrimStrings->handle(Object(Illuminate\Http\Request), Object(Closure)) #60 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ValidatePostSize.php(27): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Illuminate\Http\Request)) #61 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Illuminate\Http\Request), Object(Closure)) #62 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance.php(86): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Illuminate\Http\Request)) #63 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance->handle(Object(Illuminate\Http\Request), Object(Closure)) #64 C:\xampp\htdocs\bao-hanh-demo\vendor\fruitcake\laravel-cors\src\HandleCors.php(38): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Illuminate\Http\Request)) #65 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Fruitcake\Cors\HandleCors->handle(Object(Illuminate\Http\Request), Object(Closure)) #66 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Http\Middleware\TrustProxies.php(39): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Illuminate\Http\Request)) #67 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Http\Middleware\TrustProxies->handle(Object(Illuminate\Http\Request), Object(Closure)) #68 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}(Object(Illuminate\Http\Request)) #69 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(142): Illuminate\Pipeline\Pipeline->then(Object(Closure)) #70 C:\xampp\htdocs\bao-hanh-demo\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(111): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request)) #71 C:\xampp\htdocs\bao-hanh-demo\index.php(52): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request)) #72 {main} "}