简体   繁体   中英

Laravel: Loop on booleans to check TRUE and FALSE

I need a little help here. I have relations setup in my laravel env I want to retrieve all the slots which are marked taken .

So it's simply looping on each slot and checking if taken is true or false.

here is what I had in mind.. and been trying

$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;
  }

for this I get 200 status code but nothing displayed. Would you show me a good way to do this.

Its simply fetching all the slots of the specified id and check if they are taken . If they are not taken return the slots otherwise return all the slots

update chaining get() behind containerSlots() . Foreach is returning every thing.

[
    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
]  

You can use filter

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

你可以尝试只得到slottaken

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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