withDefault() Code Example
/** * Get the author of the post. */ public function user() { return $this->belongsTo(User::class)->withDefault(); }
Populate Default Model With Attribute
To populate the default value, you can pass in an associative array of attributes and values like below. Otherwise, you can pass in a closure and you can populate the value as shown.
/** * Get the author of the post. */ public function user() { return $this->belongsTo(User::class)->withDefault([ 'name' => 'Guest Author', ]); } /** * Get the author of the post. */ public function user() { return $this->belongsTo(User::class)->withDefault(function ($user, $post) { $user->name = 'Guest Author'; }); }
Leave a reply