$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->datetime('last_login')->nullable();
$table->rememberToken();
$table->timestamps();
});
Oct 2, 2018
16
Level 12
I want to get last logged in date from my user. The database isn't updating
Here is my table
Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->string('password'); $table->datetime('last_login')->nullable(); $table->rememberToken(); $table->timestamps(); });
AuthenticatesController
public function authenticated(Request $request, $user) { $user = auth()->user(); $user->last_login = date('Y-m-d H:i:s'); $user->save(); }
The table is still empty when i login
My user model has included last_login in fillable
Level 12
Finally used event listener
App\Listener\UpdateLastLoginAt
<?php
namespace App\Listeners;
use Illuminate\Auth\Events\Login;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Carbon\Carbon;
class UpdateLastLoginAt
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param Login $event
* @return void
*/
public function handle(Login $event)
{
$event->user->last_login_at = Carbon::now();
$event->user->save();
}
App\Providers\UpdateLastLoginAt
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Event;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
'App\Events\Event' => [
'App\Listeners\EventListener',
],
'Illuminate\Auth\Events\Login' => [
'App\Listeners\UpdateLastLoginAt',
],
];
/**
* Register any events for your application.
*
* @return void
*/
public function boot()
{
parent::boot();
//
}
}
That worked for me
Please or to participate in this conversation.