How to avoid the Trying to get property of non-object in array?

1

Previously I asked a question about the same topic, but I still have problems with some views. I have a query or a result that throws an API to me using Guzzlehttp, which throws me the following array when I print with dd ($ user):

array:1 [▼
  0 => {#187 ▶}
]

With this information:

array:1 [▼
  0 => {#187 ▼
    +"id": 555
    +"email": "[email protected]"
    +"company_id": null
    +"user_name": "Pruebas localHost"
    +"img": null
    +"user_position": null
    +"user_phone": null
    +"user_type": "US"
    +"user_validate": 0
    +"user_id_facebook": null
    +"user_conf_code": null
    +"created_at": "2017-07-28 14:05:01"
    +"updated_at": "2017-07-28 14:05:01"
  }
]

With print_r($user);

Array ( [0] => stdClass Object ( [id] => 555 [email] => [email protected] [company_id] => [user_name] => Pruebas localHost [img] => [user_position] => [user_phone] => [user_type] => US [user_validate] => 0 [user_id_facebook] => [user_conf_code] => [created_at] => 2017-07-28 14:05:01 [updated_at] => 2017-07-28 14:05:01 ) )

The problem is that when I am going to take this information to the view I can not get the results Trying to get property of non-object . I do not know what I'm doing wrong but I've used several methods and I always get the same error. the forms I've used: foreach $user[0]['id'] {{$user->id}}

This is the driver I'm using:

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());

    dd($user);
    //return view('pages.home')->with('user', $user); --> No funciona
    //return view('pages.home', compact('user')); ---> No funciona
}

In the view:

@foreach($user as $user)

     <li> {{$user->id}}" </li>

@endforeach

I also tried

<li> {{$user->id}}" </li>

How should I pass the variable correctly to my views?

    
asked by Cesar Augusto 08.08.2017 в 19:56
source

3 answers

2

You need to unpack the object

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

The result of the json_decode () function returns an array of a position;

Later you can create an empty array and assign it in the 'user' index the $ user object

$data = [];
$data['user'] = $user

return view('pages.home', $data);

In your view you will now have the object $ user available

 <li> {{$user->id}}" </li>
    
answered by 08.08.2017 / 20:26
source
1

You can also do it this way:

foreach ($users as $key => $user)
{
  $user['id'];
}

I hope it helps.

    
answered by 30.04.2018 в 18:47
0

the truth should work with $ user [0] ['id'] the other thing is that you use print_r ($ user) so you can see what type of array or dimension we are working on.

print_r($user);

Thus evidences in dimension you must point and show each of the keys

    
answered by 08.08.2017 в 20:10