Trying to get property of non-object (View :)

0

I have the following code, but when I am going to pass values to the view I get the error Trying to get property of non-object .
Does anyone know what I'm doing wrong?

In the control path:

Route::get('test2', function(){

    $client = new Client();

    $response = $client->post("http://Myruta.com/login", [

        'headers' => ['foo' => 'bar'],

        'json' => [
            'email' => '[email protected]',
            'password' => 'test',
        ]
    ]);


    $user = json_decode( $response->getBody()->getContents() );

    return view('welcome', compact('user'));


});

In the view:

{{$user->user_name}}

Result:

  

Trying to get property of non-object (View: ....

    
asked by Cesar Augusto 20.07.2017 в 18:03
source

1 answer

0

What you get according to your comment is an arrangement, with ONE OBJECT in [{}...] , therefore:

$user[0]['id']

would be enough to access the properties.

Or you can try

@foreach($user as $u) 
   {{ $u->id }}
@endforeach
    
answered by 20.07.2017 / 18:24
source