【发布时间】:2019-03-03 17:24:16
【问题描述】:
如果我执行以下操作:
App\Assignment::with(['unit.records' => function ($q) {
$q->where('process_date', '>=', '2016-01-01')
->where('process_date', '<=', '2018-09-27')
->orderBy('process_date')
->orderBy('process_hour')
->limit(1);
}])
->whereIn('id', [9])
->get();
然后我得到以下结果:
Illuminate\Database\Eloquent\Collection {#1104
all: [
App\Assignment {#1112
id: 9,
unit_id: 6,
start_date: "2015-11-25",
end_date: "2016-01-04",
unit: App\Unit {#986
id: 6,
records: Illuminate\Database\Eloquent\Collection {#945
all: [
App\Record {#853
id: 6624,
unit_id: 6,
process_date: "2017-09-19",
process_hour: 14,
},
],
},
},
},
],
}
注意加载的单元如何具有匹配查询的记录。
现在,如果我使用完全相同的查询,但将另一个赋值 (49) 添加到我的 whereIn 数组:
App\Assignment::with(['unit.records' => function ($q) {
$q->where('process_date', '>=', '2016-01-01')
->where('process_date', '<=', '2018-09-27')
->orderBy('process_date')
->orderBy('process_hour')
->limit(1);
}])
->whereIn('id', [9,49])
->get();
作业 49 的记录已显示,但作业 9 的记录不再显示:
Illuminate\Database\Eloquent\Collection {#1032
all: [
App\Assignment {#1014
id: 9,
unit_id: 6,
start_date: "2015-11-25",
end_date: "2016-01-04",
unit: App\Unit {#1283
id: 6,
records: Illuminate\Database\Eloquent\Collection {#1254
all: [],
},
},
},
App\Assignment {#1061
id: 49,
unit_id: 29,
start_date: "2017-02-24",
end_date: "9999-12-31",
unit: App\Unit {#1279
id: 29,
records: Illuminate\Database\Eloquent\Collection {#1131
all: [
App\Record {#1284
id: 6062,
unit_id: 29,
process_date: "2017-03-10",
process_hour: 13,
},
],
},
},
},
],
}
与分配 9 的条件匹配的记录显然存在,但由于某种原因,当查询发现多个分配具有与条件匹配的单元/记录时,它不会被加载。
我也用更多的分配测试了这个,在每种情况下,记录只会显示数组中的最后一个分配。
这是怎么回事?
【问题讨论】:
标签: laravel laravel-5 eloquent laravel-5.1 eager-loading