Reputation: 27
I have been using the events and listeners, but only end up to update the users table.
I tried to change the handle in listener to, $event->log->description = 'something'; but it says that
Creating default object from empty value
<?php
namespace App\Listeners;
use Illuminate\Auth\Events\Login;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Carbon\Carbon;
class LogSuccessfulLogin
{
public function __construct()
{
//
}
public function handle(Login $event)
{
$event->user->current_sign_in_at = Carbon::now();
$event->user->save();
}
}
What I'm trying to achieve is whenever a user has logged in, it should save a timestamp, description, action, to my logs table.
This is the table where I want to insert the logs.
https://i.sstatic.net/scCAh.jpg
Upvotes: 0
Views: 229
Reputation: 8750
The issue is happening because you are trying to assign a property to the $event->log
object, which does not exist.
However, to log the event, you could just use the query builder to insert a new record in the logs
table.
For example:
<?php
namespace App\Listeners;
use Carbon\Carbon;
use Illuminate\Auth\Events\Login;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\DB;
class LogSuccessfulLogin
{
public function __construct()
{
//
}
public function handle(Login $event)
{
$event->user->current_sign_in_at = Carbon::now();
$event->user->save();
$this->logEvent($event);
}
private function logEvent(Login $event)
{
return DB::table('logs')
->insert([
'user_id' => $event->user->id,
'description' => 'Some description',
'action' => 'login'
]);
}
}
Upvotes: 1