我已经在互联网上浏览了一段时间,但我无法找到我的问题的答案,这就是我在这里提问的原因。我想对用户进行身份验证以登录,但这必须使用来自两个单独表的信息来完成。我正在向我的控制器发送值:徽章和密码我正在访问它们$request->badge (note: This is the account id)$request->password (note: this is the users password)以前我尝试过以下方法:public function store(Request $request){ $request->validate([ 'badge' => 'required|int', 'password' => 'required|string', ]); $account = Account::select('id', 'user_id')->where('id', $request->badge)->first(); if(!$account) return back()->withInput()->withErrors(['password' => 'Incorrect badge or password!']); else { if(Auth::attempt(['username' => $accounts->user->username, 'password' => $request->password])) { return redirect()->route('home'); } else { return back()->withInput()->withErrors(['password' => 'Incorrect badge or password!']); } } }这将使用户登录,但是当我使用 Auth::id() 时,它返回用户的 ID 而不是帐户的 ID。例如:$request->badge 填充了 25(即帐户 ID),用户 ID 为 1。Auth::id 返回 1 而不是我想要的 25。我的表如下所示:users----idusernameemailpasswordaccounts-----iduser_idname我在帐户和用户中有关系将它们链接在一起public function accounts(){ return $this->hasMany(Accounts::class, 'user_id', 'id');}public function user(){ return $this->belongsTo(User::class, 'id', 'user_id');}我想要 auth::id 给我 25 而不是 1。
2 回答
- 2 回答
- 0 关注
- 123 浏览
添加回答
举报
0/150
提交
取消