php - Laravel detect first login and save it to Auth::user()
Get the solution ↓↓↓I want to show a user a welcoming message the first time he logs in. I have followed this question here and have created a listener file looking like this:
<?php
namespace App\Listeners;
use Illuminate\Auth\Events\Login;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
class LastLogin
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param \Illuminate\Auth\Events\Login $event
* @return void
*/
public function handle(Login $event)
{
if ($event->user->last_login === null) {
$event->user->isFirstLogin = true;
$event->user->save();
}
// Update user last login date/time
$event->user->update(['last_login' => now()]);
}
}
To test this, in my Controller I did this:
public function index()
{
$isFirstLogin = Auth::user()->isFirstLogin;
Log::info($isFirstLogin);
return view('myView');
}
However the output in my Log File is empty. How can I fix this?
This code in my listener is also not working:
public function handle(Login $event)
{
if (Auth::user()->last_login === null) {
Auth::user()->isFirstLogin = true;
Auth::user()->save();
}
// Update user last login date/time
Auth::user()->update(['last_login' => now()]);
}
In my User model I added:
//status
public $isFirstLogin = false;
Thank you for your help!
EDIT: The last_login column in my database is working perfectly
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: err_ossl_pem_no_start_line
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.