With the following model, a user has 2x relationships to a car. Both point to the same table but use a different field (daily_car_id + weekend_car_id):
class User extends Model
public function dailyCar() {
return $this->belongsTo(Car::class, 'daily_car_id', 'id');
}
public function weekendCar() {
return $this->belongsTo(Car::class, 'weekend_car_id', 'id');
}
}
This all works perfectly except for eager loading:
User::with(['dailyCar', 'weekendCar'])->findOrFail($id);
Expected
# 10 is the weekend car id and 12 is the daily
SELECT * FROM `car` WHERE `car`.`id` in (10, 12)
Actual result (incorrect)
# 10 is the weekend car id and 12 is the daily
SELECT * FROM `car` WHERE `car`.`id` in (10)
SELECT * FROM `car` WHERE `car`.`id` in (12)
Is there a way to eager load when using multiple belongsTo relationships against the same table?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire