Inserting & Updating Related Models: Belongs To Relationships

Belongs To Relationships

$account = App\Account::find(10);

$user->account()->associate($account);

$user->save();
$user->account()->dissociate();

$user->save();

Default Models

/**
 * Get the author of the post.
 */
public function user()
{
    return $this->belongsTo('App\User')->withDefault();
}
/**
 * Get the author of the post.
 */
public function user()
{
    return $this->belongsTo('App\User')->withDefault([
        'name' => 'Guest Author',
    ]);
}

/**
 * Get the author of the post.
 */
public function user()
{
    return $this->belongsTo('App\User')->withDefault(function ($user) {
        $user->name = 'Guest Author';
    });
}

Inserting & Updating Related Models: Belongs To Relationships — Structure map

Clickable & Draggable!

Inserting & Updating Related Models: Belongs To Relationships — Related pages: