when in the guzzle controller making a request to an external api.
I wanna use an id from the user who is logged in.
there is been doing the request with a static id, but now it dynamically.
I tried like this:
$science = Auth::user()->science_id;
$client = new Client([‘headers’ => [‘Accept’ => ‘application/json’]]);
$request = $client->get(
‘auth’ => [‘client’, ‘secret’],
$data = $request->getBody()->getContents();
And there is the error
500(internal server error)
and this message:
“Trying to get property ‘science_id’ of non-object”
What am i missing?
Thanks for the time
If you are using it in web app then make sure you first check if user is already authenticated by using auth middleware or manually by using Auth::check() function.
If you are trying to hit this by api that will not work here because session will not be maintained in that case. That’s why JWT tokens were introduced to maintain the state of an application.
I’ve solved it like this:
$science = auth(‘api’)->user()->science_id;
Thanks for the help!