Giter Club home page Giter Club logo

Comments (5)

kcristella avatar kcristella commented on May 13, 2024 6

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.

cleanique-coders avatar cleanique-coders commented on May 13, 2024 2

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.

sebastiandedeyne avatar sebastiandedeyne commented on May 13, 2024

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.

kcristella avatar kcristella commented on May 13, 2024

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.

freekmurze avatar freekmurze commented on May 13, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.