如何添加DESC
到默认登录 sql 查询?
我的意思是默认情况下是这样的
select * from users where name = user_name limit 1
.
我该如何添加
select * from users where name = user_name ORDER BY id DESC limit 1
?
我知道名称列应该只包含唯一值,我的登录系统不同(另一个表中的一些预定义用户),我需要多个具有相同名称的用户注册。我只想登录数据库中的最后一条记录。请帮助我如何在 laravel 中自定义模型提供程序?我不知道要修改哪些文件才能使其正常工作。
这是我的 LoginController.php 但你可以忽略它(我添加它是因为一些用户需要它)只需查看默认 loginController 来自php artisan make:auth
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Illuminate\Support\Facades\Session;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
/**
* Check either username or email.
* @return string
*/
public function login(Request $request)
{
$this->validateLogin($request);
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
if ($this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
if ($this->attemptLogin($request)) {
return $this->sendLoginResponse($request);
}
// If the login attempt was unsuccessful we will increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
$this->incrementLoginAttempts($request);
return $this->sendFailedLoginResponse($request);
}
public function username()
{
$identity = Session::get('table_id');
$fieldName = 'name';
request()->merge([$fieldName => $identity]);
return $fieldName;
}
/**
* Validate the user login.
* @param Request $request
*/
protected function validateLogin(Request $request)
{
$this->validate(
$request,
[
'password' => 'required|string',
],
[
'password.required' => 'Password is required',
]
);
}
/**
* @param Request $request
* @throws ValidationException
*/
protected function sendFailedLoginResponse(Request $request)
{
$request->session()->put('login_error', trans('auth.failed'));
throw ValidationException::withMessages(
[
'error' => [trans('auth.failed')],
]
);
}
protected function attemptLogin(Request $request)
{
$remember = true;
return $this->guard()->attempt(
$this->credentials($request), $remember
);
}
}
我的 LoginController 中的所有方法都覆盖了来自vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesUsers.php