I am trying to write an event that will display how many rows were affected by a query.
I found the Illuminate\Database\Events\QueryExecuted Event. I have added a listener for the event and registered it in Event service Provider:
EventServiceProvider.php
<?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 = [
'Illuminate\Database\Events\QueryExecuted' => [
'App\Listeners\QueryExecutedListener#handle'
],
];
/**
* Register any other events for your application.
*
* #param \Illuminate\Contracts\Events\Dispatcher $events
* #return void
*/
public function boot(DispatcherContract $events)
{
parent::boot($events);
//
}
}
and in my handle method I can see the queries being run:
public function handle(QueryExecuted $event)
{
var_dump($event);
}
However, I can't find a way to see how many rows were updated/deleted by the query run.
I'd appreciate any advice.
The EventServiceProvider included with your Laravel application provides a convenient place to register all of your application's event listeners.
https://laravel.com/docs/5.4/events
Related
Heres the event:
<?php
namespace App\Modules\Clinicians\Events;
use Illuminate\Queue\SerializesModels;
class CreateHealthCareProviderEvent
{
use SerializesModels;
public $data;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct(array $data)
{
$this->data = $data;
}
}
The event gets called just fine. But the listener:
<?php
namespace App\Modules\Clinicians\Listeners;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Modules\Clinicians\Events\CreateHealthCareProviderEvent;
use App\Modules\Clinicians\Events\EmailClinicAdminsEvent;
use App\Modules\Users\Services\RegisterUserService;
class CreateHealthCareProviderListener
{
private $registerUserService;
/**
* Create the event listener.
*
* #return void
*/
public function __construct(RegisterUserService $registerUserService)
{
$this->registerUserService = $registerUserService;
}
/**
* Handle the event.
*
* #param object $event
* #return void
*/
public function handle(CreateHealthCareProviderEvent $event)
{
$user = $this->registerUserService->setRequestData($event->data)->registerUser(Clinic::find($event->data['clinic_id']));
$user->clinician()->create([
'user_id' => $user->id,
]);
$user->clinician->clinics()->attach($event->data['clinic_id']);
event(new EmailClinicAdminsEvent($user, $event->data['clinic_id']));
}
}
Never gets called. Ever. So how am I registering these?
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* #var array
*/
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
],
// When a new health care provider registers:
CreateHealthCareProviderEvent::class => [
CreateHealthCareProviderListener::class,
],
// Email the Clinic admins when a healthcare provider registers:
// Called in the CreateHealthCareProviderListener handler method.
EmailClinicAdminsEvent::class => [
EmailClinicAdminsListener::class,
],
];
...
}
I have never had an issue registering events and listeners like this before. They always work. But for some reason the listener will not fire for this event. The event fires just fine. But not the listener. What am I missing?
I have tried:
php artisan optimize
composer dump-autoload
Nothing.
Its called as such:
// Create the health care provider user:
event(new CreateHealthCareProviderEvent($request->all()));
Any ideas? I checked spelling and namespaces and everything seems correct. What am I missing?
There are no errors, nothing. The tests still pass (I dont fake events) and the test coverage shows the event gets called, but the test coverage shows the listener does not get called. When I try this out in the browser, no user is created - which is done by the listener.
All my other events and their associated listeners are called just fine.
What is going on?
So I am retarded and will take the downvotes on this one but for future reference, make sure your use statements are proper. because Laravel won't tell you when registering events:
use App\Modules\Clinicians\CreateHealthCareProviderEvent;
use App\Modules\Clinicians\EmailClinicAdminsEvent;
use App\Modules\Clinicians\CreateHealthCareProviderListener;
use App\Modules\Clinicians\EmailClinicAdminsListener;
What do you think is missing here? let me tell you:
use App\Modules\Clinicians\Events\CreateHealthCareProviderEvent;
use App\Modules\Clinicians\Events\EmailClinicAdminsEvent;
use App\Modules\Clinicians\Listeners\CreateHealthCareProviderListener;
use App\Modules\Clinicians\Listeners\EmailClinicAdminsListener;
This is why the event was being called but not the listener. In the controller class where the event in question was being called, I imported the event properly, but in the event service provider I did not.
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 ...
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!
I am using Laravel 5.0.* and followed the following answer: login event handling in laravel 5 but I am still not able to see the event firing.
Anyone could help me with this:
This is how my Event Handler Class looks like:
<?php namespace App\Handlers\Events;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldBeQueued;
use App\User;
use Illuminate\Support\Facades\Log;
class AuthLoginEventHandler {
/**
* Create the event handler.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* #param User $user
* #param $remember
* #return void
*/
public function handle(User $user, $remember)
{
//
$user->login_counter = 1;
$user->save();
// $user->increment('login_counter');
Log::error('something wrong happened');
// dd("login fired and handled by class with User instance and remember variable");
}
}
And this is the EventServiceProvider:
<?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 handler mappings for the application.
*
* #var array
*/
protected $listen = [
'auth.login' => [
'App\Handlers\Events\AuthLoginEventHandler',
],
];
/**
* Register any other events for your application.
*
* #param \Illuminate\Contracts\Events\Dispatcher $events
* #return void
*/
public function boot(DispatcherContract $events)
{
parent::boot($events);
//
}
}
Appreciate your help
I use Laravel 5.2. I spent hours trying to figure out why my listener is not responding not knowing that I misspelled the event class name in the $listen array. Basically, if you fire your event like event(new SameEventClass(...)) make sure it is the same name you used in $listen => ['SameEventClass' => ['SameListener']]
I've got saving model event in my EventServiceProvider.php
<?php namespace App\Providers;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use App\UnprotectedLead;
class EventServiceProvider extends ServiceProvider {
/**
* The event handler mappings for the application.
*
* #var array
*/
protected $listen = [
'event.name' => [
'EventListener',
],
];
/**
* Register any other events for your application.
*
* #param \Illuminate\Contracts\Events\Dispatcher $events
* #return void
*/
public function boot(DispatcherContract $events)
{
parent::boot($events);
UnprotectedLead::saving(function($lead)
{
dd($lead->lead_phone);
$lead->lead_phone_clean = preg_replace('/[^0-9]/', '', $lead->lead_phone);
});
}
}
This code work fine on localhost, but in production saving event does not work (I use dd() to check it in prod, but script didn't die)
How to debug this? Maybe EventServiceProvider.php didn't loaded in production?
So I'm trying out the new Laravel 5 Event methodology.
In my repository, I'm firing the event "KitchenStored" as so:
// Events
use App\Events\KitchenStored;
class EloquentKitchen implements KitchenInterface {
public function store($input) {
$kitchen = new $this->kitchen;
$kitchen->name = $input['name'];
$kitchen->save();
\Event::fire(new KitchenStored($kitchen));
return $kitchen;
}
Which successfully fires this event:
<?php namespace App\Events;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
class KitchenStored extends Event {
use SerializesModels;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct($kitchen)
{
$this->kitchen = $kitchen;
}
}
However, it doesn't link up to this handler:
<?php namespace App\Handlers\Events;
use App\Events\KitchenStored;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldBeQueued;
class AttachCurrentUserToKitchen {
/**
* Create the event handler.
*
* #return void
*/
public function __construct()
{
dd('handler');
}
/**
* Handle the event.
*
* #param KitchenStored $event
* #return void
*/
public function handle(KitchenStored $event)
{
//
}
}
which I know because the dd('handler'); isn't fired during the request lifecycle.
I have registered the event with its listener here:
<?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 handler mappings for the application.
*
* #var array
*/
protected $listen = [
App\Events\KitchenStored::class => [
App\Handlers\Events\AttachCurrentUserToKitchen::class
]
];
/**
* Register any other events for your application.
*
* #param \Illuminate\Contracts\Events\Dispatcher $events
* #return void
*/
public function boot(DispatcherContract $events)
{
parent::boot($events);
Event::listen('App\Events\KitchenStored',
'App\Handlers\Events\AttachCurrentUserToKitchen');
}
}
Can anyone explain this process better so I can keep going with the cleanest code I have to date?
Many thanks
In EventServiceProvider.php, include the leading \ when referencing a class using the ::class notation:
protected $listener = [
\App\Events\KitchenStored::class => [
\App\Handlers\Events\AttachCurrentUserToKitchen::class,
],
];
You could also add use statements and keep your listener mappings short:
use App\Events\KitchenStored;
use App\Handlers\Events\AttachCurrentUserToKitchen;
...
protected $listener = [
KitchenStored::class => [
AttachCurrentUserToKitchen:class,
],
];
Or just use the string notation:
protected $listener = [
'App\Events\KitchenStored' => [
'App\Handlers\Events\AttachCurrentUserToKitchen',
],
];
If you run php artisan optimize, your event handlers should start listening.
Credit to mattstauffer from the larachat slack channel for that one.
I ran
composer dumpautoload
followed by
php artisan clear-compiled
Then my events began firing.
Don't be like me and cache your events locally to test something, then forget to clear that cache 😅
php artisan event:clear
Make sure your EventServiceProvider is calling the parent register function.
public function register() {
parent::register():
}
I had a similar issue and I fixed it by deleting the vendor\compiled.php file. Then I run "composer update" again and now the handler is firing as expected.
For me, I had the issue where I had multiple listeners for a single event. In that case, the listeners are executed in order. However, if one of the listeners return false, then all of the others listeners are not executed.
This is an older question, but I came across this same issue and for me, it was because I'd added my new Event to the service provider but crucially I had not imported it. For anyone in 2020 with this issue, check that you have imported the event.