Event listener in laravel 5.4

佐手、 提交于 2019-12-11 02:46:29

问题


I am using laravel 5.4 for creating my project and i used auth controller for login and register what I need is to get the last login time of user and store it in database when i referred that i came about an idea of creating event listeners and i done it..

login event handling in laravel 5

This is in my

EventServiceProvider.php

   <?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\SomeEvent' => [
                'App\Listeners\EventListener',
            ],
             'Illuminate\Auth\Events\Login' => [
                'App\Listeners\AuthLoginListener',
            ],
        ];

        /**
         * Register any events for your application.
         *
         * @return void
         */
        public function boot()
        {
            parent::boot();


        }
    }
I defined listener as AuthLoginListner and my 

> AuthLoginListner.php

<?php

namespace App\Listeners;
use Carbon\Carbon;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\User;
use Illuminate\Auth\Events\Login;
class AuthLoginListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  auth.login  $event
     * @return void
     */
    public function handle(Login $event)
    {
        return "yes";
    }
}

Here i just return one text now what my doubt is how its working and where i get this yes message it doesnot show me any error now have an doubt its working correcty or not if yes where i get this message ..please any one help me out i just confused with this ...


回答1:


Here is how I achieved it.

I created a new Listenener here App/Listeners/LogSuccessfullLogin.php

namespace App\Listeners;

use Illuminate\Auth\Events\Login;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use \Carbon\Carbon;

class LogSuccessfulLogin
{
    /**
    * 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 = Carbon::now();
        $event->user->save();
    }
}

and then in the EventServiceProvider, I observed for the Illuminate\Auth\Events\Login event as such:

<?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 = [
        'Illuminate\Auth\Events\Login' => [
            'App\Listeners\LogSuccessfulLogin',
        ]
    ];

    /**
    * Register any events for your application.
    *
    * @return void
    */
    public function boot()
    {
        parent::boot();

        //
    }
}

So each time a user is logging in, it will save the timestamp in the last_login field in the users table.

Hope this helps!



来源:https://stackoverflow.com/questions/44174931/event-listener-in-laravel-5-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!