Skip to content
Advertisement

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: AppModelsUser',
        'AppListenersUserSubscriber@onNewUser'
    );
}

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

Advertisement

Answer

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
    }

}

Let me know if this helped.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement