Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

theUnforgiven's avatar

Passing $var to an event & listener

Hi all,

I just want to pass the following var $companyName

$companyName = $request->input('company_name_for_submission');

Which I newup from my controller like so: event(new EmailRefereeOne($referee, $application));

To my event and listener which are as follows:

// Event
class EmailRefereeOne extends Event
{
    use SerializesModels;

    public $referee;
    /**
     * @var Applications
     */
    public $user;

    /**
     * Create a new event instance.
     *
     * @param $referee
     */
    public function __construct(References $referee, Applications $user)
    {
        $this->referee = $referee;
        $this->user = $user;
    }

    /**
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        return [];
    }


}
// Listener
class EmailRefereeOneListener implements ShouldQueue
{
    use InteractsWithQueue;

    private $referee;
    /**
     * @var Applications
     */
    private $user;

    /**
     * Create the event listener.
     *
     * @param $referee
     */
    public function __construct(References $referee, Applications $user)
    {
        $this->referee = $referee;
        $this->user = $user;
    }

    /**
     * Handle the event.
     *
     * @param  EmailRefereeOne  $event
     * @return void
     */
    public function handle(EmailRefereeOne $event)
    {
        // Handle data attributes
        $data = array(
            'worker'  => ucwords($event->user->first_name) .' '. ucwords($event->user->surname),
            'company' => '', //Need that $companyName variable here
            'email'   => $event->referee->referee_email,
            'refereeName' => $event->referee->referee_name
        );
        // Send the email
        Mail::send('emails/applications/submission', $data, function( $message ) use ($data)
        {
            $message->to($data['email'])
                    ->from(config('custom.noreplyemail'), 'Testing')
                    ->subject('Test Subject');
        });
    }

So how can I pass the $companyName variable through?

I have tried assigning $companyName but returns the following error:

BindingResolutionException in Container.php line 823:
Unresolvable dependency resolving [Parameter #2 [ <required> $companyName ]] in class App\Listeners\EmailRefereeOneListener
0 likes
2 replies
veve286's avatar
veve286
Best Answer
Level 8

@lstables You dont have to inject data in your event listener like this because you have already injected in your event.


    public function __construct(References $referee, Applications $user)
    {
        $this->referee = $referee;
        $this->user = $user;
    }

One option to achieve is you can inject in Event like this

public function __construct(References $referee, Applications $user,Request $request) { $this->referee = $referee; $this->user = $user; $this->request=$request; } and fire like this in controller ...... event(new EmailRefereeOne($referee, $application,$request));

So you can access in your listener like this $event->request->input('company_name_for_submission');

second option is easy you can directly use Request facade in Listener like this without passing anything to the event. \Request::input( );
1 like
theUnforgiven's avatar

Yeah i figured that after i posted! Lol thanks for the heads up though.

Please or to participate in this conversation.