Wednesday, March 29, 2017

How to set null if relations doesn't exist?

In Model I have:

public function publisher()
{
    return $this->belongsTo('App\User', 'user_id', 'id');
}

In template blade I try to show data User:

@foreach($announcement->offers as $key => $item)
    <img src="">
@endforeach

The Problem is that if there is no data in the User table, the program crashes because I can not get the property $item->publisher->photo.

How to fix it?



via Daniel

Advertisement