$stations_id = Station::pluck('id'); // Output [1,2,3,4]
$schedules = [];
foreach ($stations_id as $station_id) {
echo $schedules = Schedule::select('id')
->where('station_id', $station_id)
->latest()
->first();
}
$schedules
输出的回声为:
{"id":16}{"id":17}{"id":15}
但问题出在这里,我试图循环通过$schedules
从另一个名为Queue
的表中获取数据,我的意思是,我想获取我们在其中循环的每个计划的最新队列。
$queues = [];
foreach ($schedules as $schedule) {
echo $queues = Queue::withTrashed()
->latest()
->where('schedule_id', $schedule);
}
我不知道我在哪里错过了它。
发布于 2019-06-19 03:36:01
当您完成第一次循环时,变量$schedules
不是一个数组。在第一次循环之后,您将希望$schedules
成为一个数组:
$stations_id = Station::pluck('id'); // Output [1,2,3,4]
$schedules = [];
foreach ($stations_id as $station_id) {
$schedules[] = Schedule::select('id')
->where('station_id', $station_id)
->latest()
->first();
}
// second loop here
$queues = [];
foreach ($schedules as $schedule) {
$queues[] = Queue::withTrashed()
->latest()
->where('schedule_id', $schedule->getKey())->first();
}
// do something with queues
https://stackoverflow.com/questions/56655729
复制相似问题