lundi 30 novembre 2020

Getting error when fetching data through relation

I have a order table

id | amount  |   table_id

table_id is related to table like

table model

id | name

table_id can be null i have defined following relation in order table model

 public function table()
    {
        return $this->belongsTo('App\Models\Table', 'table_id', 'id');
    } 

Issue is when getting data like $orders[0]->table->name, if table_id is null then i got this error:

Trying to get property 'name' of non-object", exception: "ErrorException

if there is table_id then the data is returned but i want if there is null it should return existing data of order and null in name rather than error.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire