ST80
ST80

Reputation: 3903

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!

Upvotes: 1

Views: 2008

Answers (2)

Shah Nirjhor
Shah Nirjhor

Reputation: 49

try this on collection

$collection->filter(function ($value, $key) {
    return $value == true;
});

Upvotes: 0

TZiebura
TZiebura

Reputation: 514

Here you go :)

array_filter($result, function($item) { return $item == true });

Upvotes: 1

Related Questions