[英]Laravel: Add a custom query before login / Auth
I am new here and just started using laravel,我是新来的,刚开始使用 Laravel,
I want to add a new query before user click on the 'Login' button.我想在用户单击“登录”按钮之前添加一个新查询。
Ex.: After the user typed his username and password and clicked the Login button,例如:在用户输入他的用户名和密码并点击登录按钮后,
I want to do a query first if the username is existing on a different table.如果用户名存在于不同的表中,我想先进行查询。
if (exists) {
// do laravel default auth.
} else {
// add the username in the table
// do laravel default auth.
}
hope somebody can help me,.希望有人可以帮助我,。
Thanks谢谢
If that's the case, then you have to customize the default login method provided by the laravel.如果是这种情况,那么您必须自定义laravel 提供的默认登录方法。 see code below看下面的代码
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class LoginController extends Controller
{
/**
* Handle an authentication attempt.
*
* @param \Illuminate\Http\Request $request
*
* @return Response
*/
public function login(Request $request)
{
$credentials = $request->only('username', 'password');
$username = Othertable::where('username', $request->username)->get();
if($username->count() > 0 ) {
if (Auth::attempt($credentials)) {
// Authentication passed...
return redirect()->intended('dashboard');
}
} else {
//add the username in the table
//do laravel default auth.
}
}
}
read docs here authentication在此处阅读文档身份验证
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.