patron
patron

Reputation: 59

Accessing a request parameter already declared in constructor in LARAVEL

I want to access a Request parameter in my Listener. I found this solution (to declare it in the constructor) but it should be only initialized in handle method?

I want to access my request in the onNewUser method

my code

public function __construct(Request $request)
{
    $this->request=$request;
}

public function handle($event)
{
    //
}

public function subscribe($events)
{
    $events->listen(
        'eloquent.creating: App\Models\User',
        'App\Listeners\UserSubscriber@onNewUser'
    );
}

public function onNewUser(User$user){
    userService::userModification($user, 'creating',$this->request);
}

Upvotes: 0

Views: 737

Answers (1)

Lucas Arbex
Lucas Arbex

Reputation: 909

Have you set $request as your class's property? Like so:

class Something {

    public $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    public function onNewUser()
    {
        return $this->request; // That way, you can initialize it here
    }

}

Upvotes: 2

Related Questions