Winter CMS resources and help articles

Simple and to the point. Optimized by the community.

List filter with 3 states

0
by mjauvin, last modified on August 20th, 2023

If you need to filter a list with 3 states, you can use the "switch" filter type as below:

monthFilter:
  label: Previous / Current Month
  type: switch
  default: 0
  scope: currentOrPreviousMonth

In your model, add this scope method:

public function scopeCurrentOrPreviousMonth($query, $state)
{
    return match ($state) {
            // unfiltered query
        "0" => $query,
                // filter for state 1
        "1" => $query->previousMonth(),
                // filter for state 2
        "2" => $query->currentMonth(),
    };  
}

Discussion

0 comments

We use cookies to measure the performance of this website. Do you want to accept these cookies?