1

I need to retrieve also query string params after registration, so I'm using Request $request in both functions, but how I can pass Request also in second function?

thanks in advance!

protected function create(Request $request, array $data) // added "Request $request" for retreive querystring params
{
    $user->name = $data['name'];
    $user->surname = $data['surname'];
    $utm_source = $request->query('utm_source');
    
    $user->save();
}

public function register(Request $request)
{
    $this->validator($request->all())->validate();
    event(new Registered($user = $this->create($request->all()))); // here I'll need two pass two params...

    return $this->registered($request, $user) ?: redirect($this->redirectPath());
}
6
  • You pass request as $request only, $request->all() will give you an array not request object, so if you have to pass request pass $request and in calling function you can perform $request->all(). Commented May 12, 2021 at 8:43
  • $this->create($request, $request->all()) ? Commented May 12, 2021 at 8:43
  • @IGP Already tried this solution, I get this error: Trying to get property 'surname' of non-object Commented May 12, 2021 at 8:50
  • @Prashant Deshmukh Sorry I not understand your solution :( Commented May 12, 2021 at 8:50
  • Yes, bcs $user is not defined in create() ... ? Looks like you are just confusing yourself trying to inline everything. Take an extra line or 2, make your code more readable, close this as a typo :-) Commented May 12, 2021 at 8:53

2 Answers 2

1

Try the following:

Since you have used hinting for the $request param inside the create method it has to be an instance of the Request class.

Request::class and $request->all() are different so passing $request->all() will not work.

protected function create(Request $request)
{
    $user->name = $request->name;
    $user->surname = $request->surname;
    $utm_source = $request->query('utm_source');
    
    $user->save();
}

public function register(Request $request)
{
    $this->validator($request->all())->validate();
    event(new Registered($user = $this->create($request)));

    return $this->registered($request, $user) ?: redirect($this->redirectPath());
}
Sign up to request clarification or add additional context in comments.

1 Comment

I resolved with another simple way, adding hidden input with parameters like this {{ app('request')->input('utm_source') }}, and retreive it like others fields of form, but your solution is perfect for other causes. thanks!
0

Laravels container binding only works on controller methods that are invoked, if you call it through ordinary PHP syntax, you will need to pass each parameter.

event(new Registered($user = $this->create($request, $request->all())));

Secondly you can anywhere in your app, use the container to get your request, as long as the lifecycle has a request off course (eg. not in jobs or commands).

protected function create(array $data)
{
    $request = resolve(Request::class);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.