在 Laravel 5.6 中注册时出错
Error While Registering In Laravel 5.6
我想在应用程序中注册时遇到错误。我有一个 table 寄存器,我删除了它。当我想注册时,所有数据都转到 table。现在我希望他们去用户 table 并且我有以下功能和模型。但是当我提交要注册的信息时,弹出这个错误
QLSTATE[42S02]: Base table or view not found: 1146 Table '24gardi.registers' doesn't exist (SQL: select count(*) as aggregate from `registers` where `username` = amirhosseingheibi@gmail.com)
这是控制器:
public function ShowRegisterForm()
{
$title = "ثبت نام";
return view('register')->with('title', $title);
}
public function register(Request $request)
{
$this->validation2($request);
$register = User::create([
'username' => $request->input('username'),
'password' => bcrypt($request->input('username')),
'register_type' => $request->input('username'),
'first_name' => $request->input('username'),
'last_name' => $request->input('username'),
'gender' => $request->input('username'),
'email' => $request->input('username'),
'mobile' => $request->input('username'),
'national_id' => $request->input('username'),
'personnel_pic' => $request->input('username'),
'contract_pic' => $request->input('username'),
'national_id_pic' => $request->input('username'),
'profile_pic' => $request->input('username')
]);
if ($register) {
return redirect('/')->with('success', 'ثبت نام با موفقیت انجام شد.');
}
return redirect('/register')->with('errors', 'ثبت نام با موفقیت انجام نشد.');
}
public function validation2($request)
{
return $this->validate($request, [
'username' => 'required|unique:registers|max:255',
'password' => 'required|max:255',
'first_name' => 'required|max:255',
'last_name' => 'required|max:255',
'national_id' => 'required|unique:registers|max:10',
'gender' => 'required|max:5',
'email' => 'required|email|unique:registers|max:255',
'mobile' => 'required|max:13'
]);
}
这是路线(命名路线是在视图中使用的路线):
Route::Get('/register', 'UsersController@ShowRegisterForm');
Route::Post('/register', 'UsersController@register')->name('custom.register');
这是模型(是的,我在名为 UsersController 的控制器中添加了使用 App\User(用户 table 的模型):
protected $table = 'users';
protected $fillable = [
'register_type',
'username',
'password',
'first_name',
'last_name',
'national_id',
'gender',
'mobile',
'email',
'personnel_pic',
'national_id_pic',
'contract_pic',
'profile_pic',
'score'
];
public function tours ()
{
return $this->belongsToMany('App\Tour');
}
public function comments ()
{
return $this->hasMany('App\Comment');
}
public function suggestions ()
{
return $this->belongsToMany('App\Suggestion');
}
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'remember_token',
];
我想说的是,我不明白为什么会弹出错误。如果有人知道错误是什么,请帮助我。
您在 Validations()
中引用了错误的 table
public function validation2($request)
{
return $this->validate($request, [
'username' => 'required|unique:users|max:255',
'password' => 'required|max:255',
'first_name' => 'required|max:255',
'last_name' => 'required|max:255',
'national_id' => 'required|unique:users|max:10',
'gender' => 'required|max:5',
'email' => 'required|email|unique:users|max:255',
'mobile' => 'required|max:13'
]);
}
阅读有关验证的更多信息here
您应该将 validation2
方法中的 unique:registers
更改为
unique:users
我想在应用程序中注册时遇到错误。我有一个 table 寄存器,我删除了它。当我想注册时,所有数据都转到 table。现在我希望他们去用户 table 并且我有以下功能和模型。但是当我提交要注册的信息时,弹出这个错误
QLSTATE[42S02]: Base table or view not found: 1146 Table '24gardi.registers' doesn't exist (SQL: select count(*) as aggregate from `registers` where `username` = amirhosseingheibi@gmail.com)
这是控制器:
public function ShowRegisterForm()
{
$title = "ثبت نام";
return view('register')->with('title', $title);
}
public function register(Request $request)
{
$this->validation2($request);
$register = User::create([
'username' => $request->input('username'),
'password' => bcrypt($request->input('username')),
'register_type' => $request->input('username'),
'first_name' => $request->input('username'),
'last_name' => $request->input('username'),
'gender' => $request->input('username'),
'email' => $request->input('username'),
'mobile' => $request->input('username'),
'national_id' => $request->input('username'),
'personnel_pic' => $request->input('username'),
'contract_pic' => $request->input('username'),
'national_id_pic' => $request->input('username'),
'profile_pic' => $request->input('username')
]);
if ($register) {
return redirect('/')->with('success', 'ثبت نام با موفقیت انجام شد.');
}
return redirect('/register')->with('errors', 'ثبت نام با موفقیت انجام نشد.');
}
public function validation2($request)
{
return $this->validate($request, [
'username' => 'required|unique:registers|max:255',
'password' => 'required|max:255',
'first_name' => 'required|max:255',
'last_name' => 'required|max:255',
'national_id' => 'required|unique:registers|max:10',
'gender' => 'required|max:5',
'email' => 'required|email|unique:registers|max:255',
'mobile' => 'required|max:13'
]);
}
这是路线(命名路线是在视图中使用的路线):
Route::Get('/register', 'UsersController@ShowRegisterForm');
Route::Post('/register', 'UsersController@register')->name('custom.register');
这是模型(是的,我在名为 UsersController 的控制器中添加了使用 App\User(用户 table 的模型):
protected $table = 'users';
protected $fillable = [
'register_type',
'username',
'password',
'first_name',
'last_name',
'national_id',
'gender',
'mobile',
'email',
'personnel_pic',
'national_id_pic',
'contract_pic',
'profile_pic',
'score'
];
public function tours ()
{
return $this->belongsToMany('App\Tour');
}
public function comments ()
{
return $this->hasMany('App\Comment');
}
public function suggestions ()
{
return $this->belongsToMany('App\Suggestion');
}
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'remember_token',
];
我想说的是,我不明白为什么会弹出错误。如果有人知道错误是什么,请帮助我。
您在 Validations()
中引用了错误的 table public function validation2($request)
{
return $this->validate($request, [
'username' => 'required|unique:users|max:255',
'password' => 'required|max:255',
'first_name' => 'required|max:255',
'last_name' => 'required|max:255',
'national_id' => 'required|unique:users|max:10',
'gender' => 'required|max:5',
'email' => 'required|email|unique:users|max:255',
'mobile' => 'required|max:13'
]);
}
阅读有关验证的更多信息here
您应该将 validation2
方法中的 unique:registers
更改为
unique:users