Answers for "by default null eloquent user data in relationship in laravel 8"

PHP
0

by default null eloquent user data in relationship in laravel 8

# simple handle in blade file 
{{ $post->author->name or 'No author' }}

# Model action 
public function author()
{
    return $this->belongsTo('App\Author')->withDefault();
}

public function author()
{
    return $this->belongsTo('App\Author')->withDefault([
        'name' => 'Guest Author',
    ]);
}
Posted by: Guest on February-04-2021

Code answers related to "by default null eloquent user data in relationship in laravel 8"

Browse Popular Code Answers by Language