Geoff_S
Geoff_S

Reputation: 5105

Laravel, passing a token from controller to view

I'm confused as to why this isn't working properly here. In this controller, I'm hitting an API endpoint which, when successful, gives me back an array of tokens. Here, I'm setting one of those tokens to the accessToken variable and trying to pass that to the view to be used in another call there

Controller.php

$initialLogin = $authService->initialLogin($newPhone,$request->temp_password,$request->email,$request->password_confirmation);

//this dump and die successfully dumps the token array, so I know I'm getting my tokens
dd($initialLogin);

//Here I'm setting the variable to one of the specific tokens
$accessToken = $initialLogin->auth_access_token;

//Now I'm redirecting to a view, which I'm successfully shown
return redirect(route('auth.phone'))->with('accessToken',$accessToken);

When the process is done, after testing the dump and die which does show my tokens, then it redirects me successfully to my view. The problem here is that when dumping the variable on the view, it shows null

view.php

<?php dd($accessToken)?>

What's going on with this? I can verify that dumping $initialLogin in my controller shows the tokens, and I can verify that $accessToken is set to the specific token I need, so why isn't it properly passed?

Upvotes: 1

Views: 2265

Answers (2)

Script47
Script47

Reputation: 14550

IIRC, when using with a session variable is created, as such, make the following change:

Change,

return redirect(route('auth.phone'))->with('accessToken',$accessToken);

To

return redirect(route('auth.phone', ['accessToken' => $accessToken]));

This way, you are actually passing the variable to the view. Alternatively, If you want to continue to use with then use the session helper (session('accessToken')) to access the token.

Upvotes: 2

Magus
Magus

Reputation: 2992

When you redirect using the with helper, laravel sets your variable in the user's session, not in the view

Try this

@if(session('accessToken'))
    {{ session('accessToken') }}
@endif

Upvotes: 1

Related Questions