laravel/framework — pull request #6720 by JamShady (2014-12-18 17:02:43+00:00)
Title: [5.0] Relationships Automatically Populate Parent

Currently $parent->child->parent returns a new instance of 'Parent', as opposed to the original parent. This causes issues when a child process is updating something on the parent, because it retrieves a new instance rather than an existing one

Consider the following pseudo-code

````
class Booking extends Model {

public function invoice() {
return $this->hasOne('Invoice');
}

public function reservations() {
return $this->hasMany('Reservation');
}

}

class Reservation extends Model {

public function onDelete() {
$booking = $this->booking; // this is a new instance, unrelated to the original
$booking->invoice->total -= $this->amount;
}

}

$booking->reservations->first()->delete();
````
This won't actually update $booking->invoice->total properly because the reservation creates a new instance

This change allows you to write the following:
````
class Booking extends Model {

public function invoice() {
return $this->hasOne('Invoice')->relate('booking');
}

public function reservations() {
return $this->hasMany('Reservation')->relate('booking');
}

}

class Reservation extends Model {

public function onDelete() {
$booking = $this->booking; // this is now the original booking, so the original invoice will be correctly updated
$booking->invoice->total -= $this->amount;
}

}

$booking->reservations->first()->delete();
````

The inverse relations can be generated for
* BelongsTo / MorphTo (inheritied behaviour)
* HasOne
* HasMany
* MorphOne
* MorphMany

Previously submitted for 4.2 under #6718