Ad

Accessing A Request Parameter Already Declared In Constructor In LARAVEL

- 1 answer

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\[email protected]'
    );
}

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

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
    }

}
Ad
source: stackoverflow.com
Ad