【发布时间】:2020-01-08 02:35:54
【问题描述】:
我想用 Laravel 创建一个使用 2 个表的连接系统,实际上我的表是这样的
用户
身份证 用户名 身份证件
机构
身份证 代码 密码
我在 users 表中使用外键 id_agence
【问题讨论】:
我想用 Laravel 创建一个使用 2 个表的连接系统,实际上我的表是这样的
身份证 用户名 身份证件
身份证 代码 密码
我在 users 表中使用外键 id_agence
【问题讨论】:
$user=DB::table("Users")
->join("Agences",'Users.Id_agence','Agences.id')
->where("Users.Username","The username")
->select("Agences.password as password")
->first();
if(Hash::check("Your plain password",$user->password)){
----
}
【讨论】:
if(User::where('username', $request->username)->exists())
{
$user = DB::table('users')
->leftJoin('agences', 'users.id_agence', '=', 'agences.id')
->where('users.username', $request->username)
->first();
$auth = Hash::check($request->password, $user->password);
if($user && $auth)
{
Auth::login($user);
Session::flash('success', 'LogIn Successful!');
return redirect()->intended(route('user.index'));
}
else
{
//If unsuccessful then redirect back
Session::flash('error', 'Email or password is incorrect!');
return redirect()->back()->withInput($request->only('email',
'remember'));
}
}
- 检查通过表单或任何其他方法请求的用户名是否存在于用户数据库中。
- 如果为真,则将“代理”表与用户表连接起来,以获取所有信息以及存储的散列密码。
- 使用 Hash::check 方法可以验证与给定哈希相对应的给定纯文本。
- 检查两个条件是否为真,然后通过 Auth::login 对用户进行身份验证并将其重定向到预期的路由,否则重定向回来。
【讨论】:
public function traitement()
{
request()->validate([
'username' => ['required'],
'pass' => ['required']
]);
$result = auth()->attempt([
'username' => request('username'),
'password' => request('pass'),
]);
if($result){
flash("Your now connected")->success();
return redirect('/home');
}
return back()->withInput()->withErrors([
'pass' => 'Incorrect identifier'
]);
}
这是我首先尝试的方法
【讨论】: