Laravel 5.4 View composer with data from a $ request controller

0

I've been trying to get variables for several views for a long time. Basically what I need is to do this:

class VarComposers
{

    public function compose(View $view ){

        $user =  '555';

        $view->with('user', $user);
    }

} //class

But with the data I get here:

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 = json_decode( $response->getBody()->getContents() );

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

I do not know if I make myself understood, but I hope someone can help me.

    
asked by Cesar Augusto 04.08.2017 в 22:36
source

1 answer

0

The mechanism by which you generate the view and send data

return view( "tu-vista", compact( "datos_x" ));

saves the data in session in an internal mechanism of Laravel to make them accessible from that view that you are going to load.

If you need to load some data to have them available in several views, assuming that for example you use more than one controller to generate them, that is to say that you are going to spread them through several parts of the application, you can create your own session variable and save them there. Only you can not access them from blade directly, as if they were variables that you passed to the view. You will have access to the concrete session variable that you created. For all this, PHP is pure and hard.

If the views are generated in the same controller, theoretically, you can use it to store that data, for example through a static property, and pass it to the views as you call it.

Greetings.

    
answered by 04.08.2017 / 23:31
source