Laravel Session Return: Null

1

I need to pass variable se session to get variables in other views or controllers at the end I get a NULL

This is what I am doing:

public function login(Request $request){

    $client = new Client();

    $this->validate($request, [
        'email' => 'email|required',
        'password' => 'min:3|max:100', 
    ]);

    $response = $client->post("http://localhost:8000/v1/login", [

        'headers' => ['foo' => 'bar'],
            'json' => [
                'email' => $request['email'],
                'password' => $request['password'],
            ]
    ]);

    $user = Session::put('response', $response);

    dd($user);
}

On the screen the result is Null

In my other controller I do not get anything in the view:

public function index(){

    $user = Session::get('response');

    return view('pages.home', compact('user'));
}

What is the correct way to obtain these variables in other controllers and views?

    
asked by Cesar Augusto 04.08.2017 в 18:37
source

1 answer

1

It is correct that the result of Session::put() is NULL, the explanation is in your code:

/**
 * Put a key / value pair or array of key / value pairs in the session.
 *
 * @param  string|array  $key
 * @param  mixed       $value
 * @return void
 */
public function put($key, $value = null)
{
    if (! is_array($key)) {
        $key = [$key => $value];
    }

    foreach ($key as $arrayKey => $arrayValue) {
        Arr::set($this->attributes, $arrayKey, $arrayValue);
    }
}

Regarding the syntax for putting and taking data from the session, is correct as well , maybe you are not receiving any information when you call the API or you are trying to print the variable incorrectly in the view.

    
answered by 06.08.2017 / 16:27
source