How to use queue an event in laravel 5.2? - events

I am using event to delete particular information.
I have DeleteCourseConfirmation event listener and DeleteBranchCourse event.
It works fine.
Here is the code for DeleteBranchCourse event
class DeleteBranchCourse extends Event{
use SerializesModels;
private $fee;
private $feeId;
public function __construct($fee,$feeId)
{
$this->fee=$fee;
$this->feeId=$feeId;
}
/**
* Get the channels the event should be broadcast on.
*
* #return array
*/
public function broadcastOn()
{
return [];
}
public function deleteCourse()
{
$this->fee->destroy($this->feeId);
}}
Here is the code for DeleteCourseConfirmation event listener
class DeleteCourseConfirmation{
public function __construct()
{
}
public function handle(DeleteBranchCourse $event)
{
$event->deleteCourse();
}}
But when i tried php artisan queue:listen after implementing ShouldQueue interface in DeleteCourseConfirmation to queue event listeners
class DeleteCourseConfirmation implements ShouldQueue{
use InteractsWithQueue;
public function __construct()
{
}
public function handle(DeleteBranchCourse $event)
{
$event->deleteCourse();
}}
an error occurs.
No query results for model [App\Modules\Branch\Models\Fee]
I am following Laravel 5.2 documentation Queued Event Listeners

Found the solution.
First i changed the Event fire code
Event::fire(new DeleteBranchCourse($fee,$feeId));
to
Event::fire(new DeleteBranchCourse($feeId));
Because i don't need to send Fee model's object to DeleteBranchCourse's constructor.
and i change DeleteBranchCourse class to this
class DeleteBranchCourse extends Event{
use SerializesModels;
private $feeId;
public function __construct($feeId)
{
$this->feeId=$feeId;
}
/**
* Get the channels the event should be broadcast on.
*
* #return array
*/
public function broadcastOn()
{
return [];
}
public function deleteCourse()
{
Fee::destroy($this->feeId);
}}
And finally running this command from terminal
php artisan queue:listen
or changing the handle function of listeners class to this
public function handle(DeleteBranchCourse $event)
{
$this->attempts($event->deleteCourse());
}}

Related

I can fire events from Pusher debug console, but Laravel 5.2 isn't firing them

I am able to fire dummy events from the Pusher debug console and my client side is able to pick them up. But when I try to fire the event from my UserController nothing seems to happen.
Here is my Event class
<?php
namespace App\Events;
use App\Events\Event;
use App\Player;
use App\Product;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class NewPurchase extends Event implements ShouldBroadcast
{
use SerializesModels;
public $product;
/**
* Create a new event instance.
*
* #param Product $product
* #return void
*/
public function __construct(Product $product)
{
$this->product = $product;
}
/**
* Get the channels the event should be broadcast on.
*
* #return array
*/
public function broadcastOn()
{
return [Player::where('user_id', $this->product->seller_id)->first()->group_id];
}
}
Here is my listener, which doesn't have anything because I want everything to be process client side
<?php
namespace App\Listeners;
use App\Events\NewPurchase;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class NewPurchaseListener implements ShouldQueue
{
/**
* Create the event listener.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* #param NewPurchase $event
* #return void
*/
public function handle(NewPurchase $event)
{
//
}
}
Here is my .env
BROADCAST_DRIVER=pusher
PUSHER_APP_ID=858577
PUSHER_APP_KEY=ec160cc0a1ca15e463f4
PUSHER_APP_SECRET=
QUEUE_DRIVER=sync
Here is my event service provider
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* #var array
*/
protected $listen = [
'App\Events\NewPurchase' => [
'App\Listeners\NewPurchaseListener',
],
];
And here is where the event is fired
Event::fire(new NewPurchase($product));
My issue was that my version of Laravel hadn't been updated from the previous developers. Therefore the version of Pusher I had wasn't compatible at first with the version of Laravel I was using. I have tweaked this now and it works.

Undefined variable: event error in AppServiceProvider

I'm trying to call some function after the a laravel job gets processed and i have my code just like the example on the Docs page, but i get undefined variable event error.
<?php
namespace App\Providers;
use App\Mail\EmailProcessed;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Queue;
use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Queue\Events\JobProcessed;
use Illuminate\Support\Facades\Log;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public $event;
public function boot()
{
Queue::before(function (JobProcessing $event) {
Log::info($event->job->resolveName());
});
Queue::after(function (JobProcessed $event) {
Log::notice($event->job->resolveName());
});
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
}
I get this error
[2019-01-30 14:08:55] local.ERROR: Undefined variable: event
{"exception":"[object] (ErrorException(code: 0): Undefined
variable: event at /var/www/html/email-verification-
app/app/Providers/AppServiceProvider.php:27)
You should delete that bunch of code :
/**
* Bootstrap any application services.
*
* #return void
*/
public $event;

Laravel Queue is Processed but email not found in inbox

I Am sending emails in Laravel Queue. While using the send method, as shown here
Mail::to($userSocial->getEmail())->send(new WelcomeEmail('1234567', "haha", "Makamu"));
my email is delivered to my inbox. However when i switch to queue like below
Mail::to($userSocial->getEmail())->queue(new WelcomeEmail('1234567', "haha", "Makamu"));
I also used this method
SendEmailSocialReg::dispatch('12345678', "haha", "Makamu");
and monitor via queue:listen i am get the processing. then processed message. No error however.
What could be wrong?
my WelcomeEmail
<?php
namespace App\Mail;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class WelcomeEmail extends Mailable
{
use Queueable, SerializesModels;
public $password;
public $client_name;
public $client_email;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($password, $email, $name)
{
$this->password = $password;
$this->client_name = $name;
$this->client_email = $email;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->view('email.auto');
}
}
Your WelcomeEmail class must return markdown() at build() function like this:
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->markdown('email.auto');
}
Then the queue worker must be executed with this command:
php artisan queue:work --queue=default

Get original $request in a listener which implements ShouldQueue in Laravel 5.5

I need to get an original Request (specifically Request::server()) in my listeners for these Laravel internal events:
Illuminate\Auth\Events\Login
Illuminate\Auth\Events\Failed
Understandably, I cannot use values Request returns in my listener, since it's constructed separately server-side on queue.
Any help is highly appreciated!
In the constructor of the listener you can save the request to a member of the class, then you will be able to use it inside the handle function. For example:
class LogSuccessfulLogin implements ShouldQueue
{
protected $request;
/**
* Create the event listener.
*
* #return void
*/
public function __construct(Request $request)
{
$this->request = $request;
}
/**
* Handle the event.
*
* #param Login $event
* #return void
*/
public function handle(Login $event)
{
// here you can use $this->request->ip(); for example.
}
}

Laravel - Larabook "Method [execute] does not exist."

I'm following the Laracasts's Larabook tutorial. I'm on the 'Following Users' part. I added a follow button to all user's profiles, but I'm stuck with "BadMethodCallException Method [execute] does not exist." error. Here is my FollowsController;
<?php
use Larabook\Users\FollowUserCommand;
class FollowsController extends \BaseController {
/**
* Follow a user
*
* #return Response
*/
public function store()
{
//id of the user to follow
//id of the authenticated user
$input = array_add(Input::all(), 'userId', Auth::id());
$this->execute(FollowUserCommand::class, $input);
Flash::success('You are now following this user.');
return Redirect::back();
}
/**
* Unfollow a user
*
* #param int $id
* #return Response
*/
public function destroy($id)
{
//
}
}
According to the instructions of the laracasts/Commander package you need to inject the Commander Trait in your controller to be able to access methods like execute.
You can do this either in your BaseController so you can use it in every controller that extends BaseController:
class BaseController extends Controller {
use CommanderTrait;
}
Or just in the controller itself:
class FollowsController extends \BaseController {
use CommanderTrait;
// your methods
}

Resources