intelis
intelis

Reputation: 8068

Laravel weird Eloquent error

I cannot find the problem in this code.

public function dovoljenjaZaposlenega($id)
     {  
        echo Dovoljenja::find($id)->naziv;
     }

When I do this, i get a following error:

Trying to get property of non-object

But when i just echo $id out, it's working

public function dovoljenjaZaposlenega($id)
          { 
           echo $id;
          }

But even more weird is, that if i replace $id with actual number it's working again..

public function dovoljenjaZaposlenega($id)
     {  
        echo Dovoljenja::find(2)->naziv; // Some number..
     }


Thanks for your help!

Upvotes: 2

Views: 1403

Answers (1)

Baba
Baba

Reputation: 95161

There is nothing wrong .. you just need to basically check for errors. Dovoljenja::find($id); would only return an object if the $id exists

Try this experiment with id 2 and 4

function dovoljenjaZaposlenega($id) {
    $find = Dovoljenja::find($id);
    $find = is_object($find) ? $find->naziv : "Am empty";
    echo $find;
}

Upvotes: 6

Related Questions