// BelongsTo Default Models// Let's say you have Post belonging to Author and then Blade code:$post->author->name; // Of course, you can prevent it like this:$post->author->name ?? ''// or@$post->author->name // But you can do it on Eloquent relationship level:// this relation will return an empty App\Author model if no author is attached to the postpublic function author() { return $this->belongsTo(Author::class)->withDefault();}// orpublic function author() { return $this->belongsTo(Author::class)->withDefault([ 'name' => 'Guest Author' ]);}
Tip given by @coderahuljat
Enjoyed This Tip?
Get access to all premium tutorials, video and text courses, and exclusive Laravel resources. Join our community of 10,000+ developers.
Recent Courses on Laravel Daily
AI Agents/IDEs for Laravel: May 2026 (Claude Code, Codex, OpenCode, etc)
7 lessons
52 min
Roles and Permissions in Laravel 13
14 lessons
57 min
Laravel 13 Eloquent: Expert Level
41 lessons
1 h 34 min