首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Laravel:为foreach()提供的参数无效

Laravel:为foreach()提供的参数无效
EN

Stack Overflow用户
提问于 2019-06-19 03:21:32
回答 1查看 623关注 0票数 0
代码语言:javascript
复制
$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的表中获取数据,我的意思是,我想获取我们在其中循环的每个计划的最新队列。

代码语言:javascript
复制
$queues = [];
foreach ($schedules as $schedule) {       
    echo $queues = Queue::withTrashed()
                 ->latest()
                 ->where('schedule_id', $schedule);               
}

我不知道我在哪里错过了它。

EN

回答 1

Stack Overflow用户

发布于 2019-06-19 03:36:01

当您完成第一次循环时,变量$schedules不是一个数组。在第一次循环之后,您将希望$schedules成为一个数组

代码语言:javascript
复制
$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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56655729

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档