-
Notifications
You must be signed in to change notification settings - Fork 11.5k
Closed
Closed
Copy link
Labels
Description
- Laravel Version: 6.0.3
- PHP Version: 7.1.24 (also with 7.3)
- Database Driver & Version: MySQL 5.7.24
Description:
When eager loading a relationship (see example below), I am unable to disable this behavior using:
->without('items.event')
A "fix" that works, but is counter-intuitive is:
->with(['items' => function ($related) {
$related->without('event');
}])
It looks like the nested, eager loaded query does not take the parent scope into account when deciding which relationships to query.
Steps To Reproduce:
Given the following setup:
class Event extends Model {
public function items() {
return $this->hasMany(Item::class);
}
}
class Item extends Model {
protected $with = ['event'];
public function event() {
return $this->belongsTo(Event::class);
}
}
and the following query:
$items = Event::query()->with('item')->get();
It will create an additional query to load the event data back into the items.
SELECT * FROM `events`
SELECT * FROM `items` WHERE `items`.`event_id` in (1, 2)
SELECT * FROM `events` WHERE `events`.`id` in (1, 2) # <- Additional, unnecessary query
roelmagdaleno, aamsur-mkt, estimtrack, Yiddishe-Kop and michael-caraccio-colorix