Updated 03/02/2016
You’ll also need to make sure you tell your Events listeners to listen appropriately:
<?php namespace App\Providers; use Illuminate\Contracts\Events\Dispatcher as DispatcherContract; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; class EventServiceProvider extends ServiceProvider { /** * The event listener mappings for the application. * * @var array */ protected $listen = [ 'auth.login' => [ 'App\Listeners\AuthLoginListener', ] ]; /** * Register any other events for your application. * * @param \Illuminate\Contracts\Events\Dispatcher $events * * @return void */ public function boot(DispatcherContract $events) { parent::boot($events); // } }
If you need to store the user_id
(or any value for that matter) as a cookie when a User logs in, simply add (or edit) the app/Listeners/AuthLoginListener.php
to include the following:
<?php namespace App\Listeners; use App\Events\AuthLoginEvent; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Cookie\CookieJar; use App\User; class AuthLoginListener { /** * Create the event listener. * * @return void */ public function __construct() { // } /** * Handle the event. * * @param AuthLoginEvent $event * * @return void */ public function handle(AuthLoginEvent $event) { $user = User::find($event->user->id); // find the user associated with this event $user->last_online_date = new \DateTime; $user->save(); $cookieJar = new CookieJar(); $cookieJar->queue(cookie('user_id', $user->id)); } }
2 Comments
Mikael Agabalyants · July 14, 2017 at 1:02 am
This isn’t work
Jake Litwicki · July 20, 2017 at 3:22 pm
Do you have any errors or specific issues to help troubleshoot?