Comments (5)
Thanks for the suggestions. I didn't realize that Laravel broadcasts events for login activity. So with that known, I ended creating a listener for the Login event and called activity()->log('Login successful');
in the handle method of that listener.
from laravel-activitylog.
What I did, simply create an Event & a Listener.
Here my LogEvent
<?php
namespace App\Events;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class LogEvent extends Event
{
use SerializesModels;
public $subject;
public $description;
public $user;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($user, $subject, $description)
{
$this->user = $user;
$this->subject = $subject;
$this->description = $description;
}
/**
* Get the channels the event should be broadcast on.
*
* @return array
*/
public function broadcastOn()
{
return [];
}
}
And for my LogEvent
listener:
<?php
namespace App\Listeners;
use App\Events\LogEvent;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Spatie\Activitylog\Models\Activity;
class LogEvent
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param LogEvent $event
* @return void
*/
public function handle(LogEvent $event)
{
activity($event->subject)
->by($event->user)
->log($event->description);
}
}
So, later on I just call event(new \App\Events\LogEvent(Auth::user(), 'Authentication', 'Successfully logged in.'));
to start logging.
from laravel-activitylog.
For most cases, I'd suggest simply manually calling the activity()
function in your controllers where the actions are happening.
Another strategy would be to have your application emit events for login/logout/etc., and setting up a listener to log the actions.
There's not really a generic way-to-go here I think, it depends on your application.
from laravel-activitylog.
Thanks - I think for the use case of logging Laravel out of the box login specifically, the event approach must be used as the action happens in the framework source as opposed to the Auth controller. Is that correct?
from laravel-activitylog.
Thanks for posting your approach @cleanique-coders
Here's how we do it for our own projects: https://github.com/spatie-custom/blender/blob/master/app/Http/Controllers/Back/AuthController.php#L31
from laravel-activitylog.
Related Issues (20)
- Logs not populated when running db:seed HOT 2
- Unable To Query Morph To Relationships + Nested Eager Loading Morph To Relationships HOT 1
- Log tags in resource HOT 1
- Update logs empty nulls HOT 3
- Wrong minimal Laravel version in package.json HOT 3
- Phone number field is not checked as a string and does not update correctly HOT 2
- Delete method not resulting in log HOT 2
- Nested null relation throws TypeError HOT 1
- Configure more than one auth driver HOT 1
- I want only manual update value log not automatic update value log HOT 2
- Update documentation website HOT 3
- Object of class Illuminate\Support\Carbon could not be converted to int HOT 1
- Laravel-activitylog not installing HOT 5
- Documentation for V3 and V4 missing from Spatie website HOT 1
- Not saving Auth user information when I use with model operations. HOT 1
- Bulk Update Logging Not working
- Properties empty []
- Properties empty [] HOT 1
- updateOrCreate() does not log anything HOT 1
- Log many to many doesn't work
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-activitylog.