Skip to content
Advertisement

Laravel how to check if at least one column value is true or false

How can I check, if at least one of a bunch of checkboxes is checked/true? I’m using Laravel 5.8

My query right now:

$preferences = DB::table("preferences")
    ->select('day', 'evening', 'night', 'weekend', 'full_time', 'part_time')
    ->where(["user_id" => request()->user()->id])
    ->get();

return response(['success' => true, "preferences" => $preferences]);

So far this works, but “only” returns an array with each value. Like this:

[
  {
    day: 0,
    evening: 1,
    night: 0,
    weekend: 0,
    full_time: 1,
    part_time: 0
  }
]

I need some kind of value that tells me : is-checked: true/false – something like that.

How can I achieve this? Any suggestion is welcome!

Advertisement

Answer

Here you go 🙂

array_filter($result, function($item) { return $item == true });
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement