Php Laravel问题上次登录时,每行都有时间戳
我正在使用Laravel身份验证事件进行最后一次登录,但当时间戳标记到每一行时出现问题 “上次登录”列中的我的代码 我希望它只记录在已登录的用户中。但看起来每个用户的登录时间都会被节省。 正如我所附的照片 app\Listeners\UpdateLastSignInAtPhp Laravel问题上次登录时,每行都有时间戳,php,laravel,laravel-5.8,Php,Laravel,Laravel 5.8,我正在使用Laravel身份验证事件进行最后一次登录,但当时间戳标记到每一行时出现问题 “上次登录”列中的我的代码 我希望它只记录在已登录的用户中。但看起来每个用户的登录时间都会被节省。 正如我所附的照片 app\Listeners\UpdateLastSignInAt <?php namespace App\Listeners; use Illuminate\Auth\Events\Login; use Illuminate\Queue\InteractsWithQueue
<?php
namespace App\Listeners;
use Illuminate\Auth\Events\Login;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Carbon\Carbon;
class UpdateLastSignInAt
{
public function __construct()
{
//
}
public function handle(Login $event)
{
$event->user->LAST_LOGIN = Carbon::now();
$event->user->save();
}
}
<?php
namespace App\Listeners;
use Illuminate\Auth\Events\Login;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Carbon\Carbon;
class UpdateLastSignInAt
{
public function __construct()
{
//
}
public function handle(Login $event)
{
$date = Carbon::now();
$user = $event->user->ID;
User::where('ID', $user)
->update(['LAST_LOGIN' => $date]);
}
}
}为什么不重写authController中的登录方法????? 像这样
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 (method_exists($this, 'hasTooManyLoginAttempts') &&
$this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
if ($this->attemptLogin($request)) {
$user = auth()->guard('web')->user();
//$user = auth()->guard('api')->user();
$user->last_login = now()->toDateTimeString();
$user->save();
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);
}
为什么不重写authController中的登录方法????? 像这样
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 (method_exists($this, 'hasTooManyLoginAttempts') &&
$this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
if ($this->attemptLogin($request)) {
$user = auth()->guard('web')->user();
//$user = auth()->guard('api')->user();
$user->last_login = now()->toDateTimeString();
$user->save();
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);
}
最后,我可以做到这一点,具体来说,我使用WHERE-into函数句柄 app\Listeners\UpdateLastSignInAt
<?php
namespace App\Listeners;
use Illuminate\Auth\Events\Login;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Carbon\Carbon;
class UpdateLastSignInAt
{
public function __construct()
{
//
}
public function handle(Login $event)
{
$event->user->LAST_LOGIN = Carbon::now();
$event->user->save();
}
}
<?php
namespace App\Listeners;
use Illuminate\Auth\Events\Login;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Carbon\Carbon;
class UpdateLastSignInAt
{
public function __construct()
{
//
}
public function handle(Login $event)
{
$date = Carbon::now();
$user = $event->user->ID;
User::where('ID', $user)
->update(['LAST_LOGIN' => $date]);
}
}
最后我可以做了,具体来说,我使用WHERE-into函数句柄
app\Listeners\UpdateLastSignInAt
<?php
namespace App\Listeners;
use Illuminate\Auth\Events\Login;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Carbon\Carbon;
class UpdateLastSignInAt
{
public function __construct()
{
//
}
public function handle(Login $event)
{
$event->user->LAST_LOGIN = Carbon::now();
$event->user->save();
}
}
<?php
namespace App\Listeners;
use Illuminate\Auth\Events\Login;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Carbon\Carbon;
class UpdateLastSignInAt
{
public function __construct()
{
//
}
public function handle(Login $event)
{
$date = Carbon::now();
$user = $event->user->ID;
User::where('ID', $user)
->update(['LAST_LOGIN' => $date]);
}
}
查看在用户
变量中获取用户
并在
更新上次登录时的是否有效$user=$event->user
$user->LAST_LOGIN..
$user->save
如果您使用的是Carbon::now()
在侦听器中,它将使用当前时间,即作业处理的时间。查看在user
变量中获取user
并更新上次登录时的是否有效$user=$event->user
$user->LAST_LOGIN..
$user->save
如果您在侦听器中使用Carbon::now()
,它将以当前时间作为作业处理的时间。这是不同的尝试。他正试图把工作排成队。但您建议的是一个同步过程,最好将职责分配给不同的类。在一个方法中做不同的事情将违反良好的设计模式。对不起,我的问题是登录时。应将时间添加到已登录的用户。但是每个用户都添加了它您的登录情况如何?@Mehdi使用Auth::trunt
登录这是不同的尝试。他正试图把工作排成队。但您建议的是一个同步过程,最好将职责分配给不同的类。在一个方法中做不同的事情将违反良好的设计模式。对不起,我的问题是登录时。应将时间添加到已登录的用户。但是每个用户都添加了它您的登录情况如何?@Mehdi使用Auth::trunt登录