在Laravel 5.6中注册时出错

时间:2018-03-15 09:37:41

标签: mysql laravel laravel-5.6

我想在应用中注册时出错。我有一个表寄存器,我删除它。当我想注册时,所有数据都会转到该表。现在我想让他们去用户表,我有以下功能和模型。但是当我提交要注册的信息时,会弹出此错误

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(用户表的模型)):

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',
    ];

我想说的是,我不会动摇,为什么错误弹出。如果有人知道错误对我有帮助,请。

2 个答案:

答案 0 :(得分:1)

您正在参考Validations()

中的错误表格
 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

答案 1 :(得分:0)

您应该将unique:registers方法中的validation2更改为 unique:users