Undefined variable: event error in AppServiceProvider - laravel

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;

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.

Getting error routeNotificationFor() on laravel notifications

I'm trying to send email to members on my app and I get the following error:
production.ERROR: Call to undefined method
Illuminate\Database\Query\Builder::routeNotificationFor()
{"exception":"[object] (BadMethodCallException(code: 0): Call to
undefined method
Illuminate\Database\Query\Builder::routeNotificationFor() at
/var/www/vhosts/.../laravel/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2483)
I have the members model with the Notifiable trait and the weird issue is that on my local machine the emails are delivered... The problem is on production...
Any ideas?
The notifications are fired with:
Notification::send($members_to_notify, new CommMessage($communication));
And the CommMessageclass is:
CommMessage.php
namespace App\Notifications\Communications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use App\Mail\Communications\Message as Mailable;
class CommMessage extends Notification implements ShouldQueue
{
use Queueable;
private $communication;
/**
* Create a new notification instance.
*
* #return void
*/
public function __construct($communication)
{
$this->communication = $communication;
}
/**
* Get the notification's delivery channels.
*
* #param mixed $notifiable
* #return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* #param mixed $notifiable
* #return Mailable
*/
public function toMail($notifiable)
{
return (new Mailable($this->communication))->to($notifiable->email);
}
}
The mailable Message class is:
Message.php
namespace App\Mail\Communications;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class Message extends Mailable
{
use Queueable, SerializesModels;
/**
* The appointment instance.
*
* #var Appointment
*/
public $communication;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($communication)
{
$this->communication = $communication;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
$this->replyTo($this->communication->school->email)
->markdown('emails.communications.message');
$this->subject($this->communication->title);
$this->withSwiftMessage(function ($message) {
$message->getHeaders()
->addTextHeader('X-Mailgun-Variables', json_encode([
'model_id' => $this->communication->id,
'model' => get_class($this->communication),
'email_type' => 11 //Communication message (EmailLogType)
]));
});
}
}
This issue was solved. Code has no errors, I was using Supervisor to manage queues and I have forgot to restart it on production server.

How to bind Services in Laravel?

Suppose I have a service: App\Services\FooService.
To use this service I need to bind it using this statement:
$this->app->bind('FooService', \App\Services\FooService::class);
So where do I need to put the above statement? In which file?
you need to bind in AppServiceProvider
app/Providers/AppServiceProvider.php
namespace App\Providers;
use App\SocialProvider;
use App\TwitterSocialProvider;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
$this->app->bind('FooService', \App\Services\FooService::class);
}
}

When i am registering user observer in Laravel while fetching single user also i'm getting list of all users

Getting issue with Laravel Observer
I created a user observer when i'm doing api call in order to fetch single user also i'm getting list of all user exist in table. what i'm doing wrong.
Here my ObserverProvider and UserObserver which i register in app.php
ObserversServiceProvider
namespace App\Providers;
use App\User;
use App\Observers\UserObserver;
use Illuminate\Support\ServiceProvider;
class ObserversServiceProvider extends ServiceProvider {
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot() {
User::observe(UserObserver::class);
}
/**
* Register the application services.
*
* #return void
*/
public function register() {
}
}
UserObserver
namespace App\Observers;
use App\User;
class UserObserver
{
/**
* Listen to the User created event.
*
* #param User $user
* #return void
*/
public function created(User $user) {
// doing something
}
}
I'm getting list of all user when i'm doing.
$user = User::where('user_id',1)->first();

Laravel 5.3 Call to a member function groupBy() on null

I'm getting the following error:
FatalErrorException in EloquentVehicle.php line 30: Call to a member
function groupBy() on null
I have the following code:
<?php
namespace App\Project\Frontend\Repo\Vehicle;
use Illuminate\Database\Eloquent\Model;
class EloquentVehicle implements VehicleInterface
{
protected $vehicle;
/**
* EloquentVehicle constructor.
*
* #param Model $vehicle
*/
public function __construct(
Model $vehicle
)
{
$this->$vehicle = $vehicle;
}
/**
* Fetch unique makes
*
* #return mixed
*/
public function fetchMakes()
{
return $this->vehicle->groupBy(array('make'))
->orderBy('make', 'asc')
->get();
}
}
I've checked Illuminate\Database\Eloquent\Model for the method which is obviously not there, but I don't know what I should be adding to my class so that I can use the groupBy method. The laravel docs say the method exists.
UPDATE: Apparently I can't typehint an abstract class. I don't know how else I should be going about using Eloquent to retrieve records. If it helps, below is the code I have for registering the classes to the service container
<?php
namespace App\Providers;
use App\Vehicle;
use App\Project\Frontend\Repo\Vehicle\EloquentVehicle;
use Illuminate\Support\ServiceProvider;
class RepoServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
$this->app->bind('App\Project\Frontend\Repo\Vehicle\VehicleInterface', function($app)
{
return new EloquentVehicle(
new Vehicle
);
});
}
}
I just found my mistake and quite literally lay my face in my palms.
This
$this->$vehicle = $vehicle;
should be this
$this->vehicle = $vehicle;

Resources