Laravel Event within a Command not being fired - laravel

Here's a simplified version of my Command which attempts to fire an event (obviously there's much more logic in the original).
The event is not being fired. Any idea what I've missed?
<?php namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Events\StackEvent;
class StackCommand extends Command
{
public function handle()
{
event(new StackEvent());
}
}
Thanks

Related

how to broadcast event called by job class

In a Laravel project i am trying to broadcast a event named "closed" via pusher.
In my controller i am calling:
App\Jobs\Closing::dispatch();
My App\Jobs\Closing.php:
<?php
namespace App\Jobs;
use App\Jobs\Close;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class Closing implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct(){}
public function handle()
{
$delay = mt_random(10,20);
Close::dispatch()->delay(now()->addSeconds($delay));
}
}
My app\Jobs\Close.php:
<?php
namespace App\Jobs;
use App\Events\Closed;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class Close implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct(){}
public function handle()
{
event(new Closed());
}
}
My App\Events\Closed.php:
<?php
namespace App\Events;
use Illuminate\Queue\SerializesModels;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
class Closed implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public function __construct(){}
public function broadcastOn()
{
return ['updates'];
}
public function broadcastAs()
{
return 'closed';
}
public function broadcastWith()
{
return ['message' => 'Is Closed!'];
}
}
At this point i need to explain two mandatory situations.
The first one is that i needed to create the job Close class as a job because i needed to delay the execution of the task close. And the only way i can see to make this "delay" is with a Job Class.
The second one is that i have chosen to implement "ShouldBroadcastNow" instead of "ShouldBroadcast" in the event Closed class because i don't want to queue the broadcasting.
Now the problem is that after running:
php artisan queue:work --tries=1
i get in the following output on Command Console:
Processing: App\Jobs\Closing
Processed: App\Jobs\Closing
Processing: App\Jobs\Close
Processing: App\Events\Closed
Failed: App\Events\Closed
Failed: App\Jobs\Close
The first thing that i find weird is that App\Events\Closed goes to queue despite the fact that it implements "ShouldBroadcastNow".
On laravel.log it seems that it occurred a BroadcastException at PusherBroadcaster.php.
But if in the Controller i do:
event(new App\Events\Closed());
the event is properly broadcast via pusher to the client browser.
What is going wrong?
Is there other way do delay the "close" without jobs?
The purpose is to have the following workflow:
1 - We have a event named "closing" an another event named "closed";
2 - We have a task named "close" that occurs "x" seconds after the event "closing", where "x" is a random number;
3 - After the execution of "close" task we broadcast the event "closed".
Thank you for your attention to my problem
Meanwhile i found this is a already known issue (https://github.com/laravel/framework/issues/16478).
It can be solved in two ways:
1 - editing the relevants php.ini files (in my case it was mamp/conf/php7.0.9/php.ini and mamp/bin/php/php7.0.9/php.ini) to point at the location of curl certificate;
2 - editing config/broadcasting.php (setting encrypted to false in pusher options).

Call to undefined method Illuminate\Notifications\Notification::send()

I am trying to make a notification system in my project.
These are the steps i have done:
1-php artisan notifications:table
2-php artisan migrate
3-php artisan make:notification AddPost
In my AddPost.php file i wrote this code:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class AddPost extends Notification
{
use Queueable;
protected $post;
public function __construct(Post $post)
{
$this->post=$post;
}
public function via($notifiable)
{
return ['database'];
}
public function toArray($notifiable)
{
return [
'data'=>'We have a new notification '.$this->post->title ."Added By" .auth()->user()->name
];
}
}
In my controller I am trying to save the data in a table and every thing was perfect.
This is my code in my controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
use App\User;
//use App\Notifications\Compose;
use Illuminate\Notifications\Notification;
use DB;
use Route;
class PostNot extends Controller
{
public function index(){
$posts =DB::table('_notification')->get();
$users =DB::table('users')->get();
return view('pages.chat',compact('posts','users'));
}
public function create(){
return view('pages.chat');
}
public function store(Request $request){
$post=new Post();
//dd($request->all());
$post->title=$request->title;
$post->description=$request->description;
$post->view=0;
if ($post->save())
{
$user=User::all();
Notification::send($user,new AddPost($post));
}
return redirect()->route('chat');
}
}
Everything was good until I changed this code:
$post->save();
to this :
if ($post->save())
{
$user=User::all();
Notification::send($user,new AddPost($post));
}
It started to show an error which is:
FatalThrowableError in PostNot.php line 41: Call to undefined method
Illuminate\Notifications\Notification::send()
How can i fix this one please??
Thanks.
Instead of:
use Illuminate\Notifications\Notification;
you should use
use Notification;
Now you are using Illuminate\Notifications\Notification and it doesn't have send method and Notification facade uses Illuminate\Notifications\ChannelManager which has send method.
Using this
use Illuminate\Support\Facades\Notification;
instead of this
use Illuminate\Notifications\Notification;
solved the problem for me.
Hope this helps someone.
using this is better
use Notification
Instead of
use Illuminate\Support\Facades\Notification
this makes the send() not accessible [#Notification Databse]

Laravel events not firing?

I can't seem to get any of my eloquent.saved event handlers to run.
I tried this:
<?php
namespace App\Providers;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
protected $listen = [
'eloquent.saved: \App\Models\Company' => [
'\App\Models\Company#hasSaved',
],
];
}
And then added this method to \App\Models\Company:
public function hasSaved() {
die("SAVED!!!");
}
But it doesn't run when I save a company.
I tried creating an observer:
<?php
namespace App\Providers;
use App\Models\Company;
use App\Observers\CompanyObserver;
use DB;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
Company::observe(CompanyObserver::class);
}
}
But the events never fire:
<?php namespace App\Observers;
class CompanyObserver {
public function saved() {
die('saved');
}
public function saving() {
die('saving');
}
}
I tried using a listener class in EventServiceProvider instead:
protected $listen = [
'eloquent.saved: \App\Models\Company' => [
\App\Listeners\CompanySavedListener::class,
],
];
But that also never runs.
Lastly, I tried adding this to EventServiceProvider
public function boot(DispatcherContract $events)
{
parent::boot($events);
$events->listen('eloquent.*', function() {
dump(func_get_args());
});
}
And that does fire a bunch of random events, but it's just feeding me model instances -- I have no idea what events are actually firing.
So what's going on? I just want to know when my Company has saved.
Let's go for Observer way. The problem is that you used:
Company::observe(CompanyObserver::class);
in register method of AppServiceProvider and you should use it in boot method. When you move this line to boot method (of same class) it will work without a problem and when you save Company, code from saved method of CompanyObserver should be launched.

Trigger to Pusher.com does nothing

I am trying to use vinkla/pusher on Laravel 5.1
This is what I've added to app.php:
Vinkla\Pusher\PusherServiceProvider::class as a service provider
'LaravelPusher' => Vinkla\Pusher\Facades\Pusher::class, as a facade.
Route:
Route::get('/api/bid', [
'uses' => 'APIController#bid'
]);
And this is the controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\CurrentAuction;
use App\User;
use App\Bid;
use Session;
use LaravelPusher;
class APIController extends Controller
{
public function getCurrentAuction()
{
// snip...
}
public function bid(User $user) {
// Whole heap of things done with $user...
// snip...
$data['bids'] = 1;
LaravelPusher::trigger('bid_channel', 'NewBid', $data);
}
}
Calling that method does everything except trigger the pusher event.
I don't understand what I've done wrong.
Any help would be greatly appreciated. Thanks!
It appears that when using Laravel Homestead/Vagrant, pusher, broadcasting or anything like that doesn't want to work for me.
I pushed everything up to a live server and it worked without any code changes.

Controlling the tubes for Queued Events in Laravel 5

So I've started using Queued Events in L5 for handling some logic and I was wondering if it was possible to tell laravel what tube to use when pushing the events onto Beanstalkd.
I couldn't see anything in the documentation about it.
After digging through the Event Dispatcher code.
I found that if there is a queue method on your event handler laravel will pass the arguments through to that method and let you call the push method manually.
So if you have a SendEmail event handler you can do something like this:
<?php namespace App\Handlers\Events;
use App\Events\UserWasCreated;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldBeQueued;
class SendEmail implements ShouldBeQueued {
use InteractsWithQueue;
public function __construct()
{
}
public function queue($queue, $job, $args)
{
// Manually call push
$queue->push($job, $args, 'TubeNameHere');
// Or pushOn
$queue->pushOn('TubeNameHere', $job, $args);
}
public function handle(UserWasCreated $event)
{
// Handle the event here
}
}

Resources