0

I'm trying to make a select in database and return as json, but i'm receiving:

Undefined property: stdClass::$id_cores

My function:

 public function show($id)
    {

        $animaisPerdidos = DB::select('SELECT a.* FROM cademe47_cademeupet.animais_perdidos a INNER JOIN cademe47_cademeupet.users u ON (a.id_usuario = u.id) WHERE u.senha = ?', [$id]);

        for($i = 0; $i < count($animaisPerdidos); $i++){
            $animaisPerdidos[$i]->id_cores = unserialize($animaisPerdidos[$i]->id_cores);
        }

        return response()->json($animaisPerdidos);
    }

if i put on var_dump in $animaisPerdidos is show:

array(4) {
  [0]=>
  object(stdClass)#235 (14) {
    ["id"]=>
    int(43)
    ["id_usuario"]=>
    int(24)
    ["lat"]=>
    string(10) "-20.545534"
    ["lng"]=>
    string(10) "-47.407395"
    ["id_tipo_pet"]=>
    int(2)
    ["data"]=>
    string(10) "2019-04-17"
    ["informacoes_adicionais"]=>
    string(9) "asdasdasf"
    ["updated_at"]=>
    string(10) "2019-05-10"
    ["created_at"]=>
    string(10) "2019-05-10"
    ["nome_identificador"]=>
    string(10) "dsadasdasd"
    ["foto"]=>
    string(25392) "iVBOR..."
    ["telefone_contato"]=>
    string(11) "16981828406"
    ["email_contato"]=>
    string(22) "dsadasdsa@gmail.com.br"
    ["animal_pendente"]=>
    int(3)
  }...

How i can fix this? i already try using the $animaisPerdidos[$i][$id_cores] syntax, but anyway i receive other error.

  • 4
    The message and the output of your `var_dump` make it quite clear, there is no `id_cores` property in your data. Why are you expecting there to be one? – Nick May 23 '19 at 00:37
  • Mind doing `print_r($animaisPerdidos)` or `var_dump($animaisPerdidos)` ? – Sulthan Allaudeen May 23 '19 at 00:39
  • Shouldn't you be doing `$animaisPerdidos[$i]['id_cores']` instead of `$animaisPerdidos[$i]->id_cores`? – Grant C. May 23 '19 at 00:53
  • What kind of answer are you looking for? Based on the information provided, the error is clear, you're trying to access a property that doesn't exist - you should probably add it to the database or reference another variable. But it's unclear what the intended output should be – HPierce May 23 '19 at 01:25
  • Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – miken32 May 23 '19 at 03:02

0 Answers0