繁体   English   中英

Laravel:在布尔值上循环以检查TRUE和FALSE

[英]Laravel: Loop on booleans to check TRUE and FALSE

我需要一点帮助。 我有关系建立在我的laravel ENV我要检索的所有slots被标记taken

因此,它只是在每个插槽上循环并检查taken是true还是false。

这就是我的想法..并一直在尝试

$plateCont = \App\Models\PlateContainer::find(7);
  return   $freeslots = $plateCont->containerSlots()->get();


  foreach ($freeslots as $slot)
  {
    if($slot->where('taken', false))
    {
      return $slot->lists('slot');
    }
    // return $slots;
  }

为此,我得到了200状态代码,但未显示任何内容。 您能告诉我一种执行此操作的好方法吗?

它只是获取指定id所有插槽,并检查它们是否被taken 如果不使用taken返回插槽,否则返回all插槽

更新 containerSlots()后面的链接get() containerSlots() Foreach正在归还所有东西。

[
    1,
    7,
    9,
    5,
    3,
    5,
    4,
    3,
    2,
    1,
    5,
    1,
    8,
    9,
    8,
    5,
    5,
    1,
    8,
    5,
    1,
    5,
    8,
    8,
    4,
    5,
    8,
    4,
    2,
    1
]  

您可以使用过滤器

$freeslots = $plateCont->containerSlots->filter(function($item) {
    return $item->taken;
})

你可以尝试只得到slottaken

$freeslots = $plateCont->containerSlots()->where('taken', true)->get()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM