1// BelongsTo Default Models 2// Let's say you have Post belonging to Author and then Blade code: 3$post->author->name; 4 5// Of course, you can prevent it like this: 6$post->author->name ?? '' 7// or 8@$post->author->name 9 10// But you can do it on Eloquent relationship level:11// this relation will return an empty App\Author model if no author is attached to the post12public function author() {13 return $this->belongsTo('App\Author')->withDefault();14}15// or16public function author() {17 return $this->belongsTo('App\Author')->withDefault([18 'name' => 'Guest Author'19 ]);20}
Tip given by @coderahuljat