Filters

Lamotivo filters allow you to scope your Lamotivo index queries with custom conditions.

Select Filters

The most common type of Lamotivo filter is the select filter, which allows the user to select a filter option from a drop-down list. You may generate a select filter using the lm:filter Artisan command. By default, Lamotivo will place newly generated filters in the app/Lm/Filters directory:

php artisan lm:filter UserType

Each select filter generated by Lamotivo contains two methods: apply and options. The apply method is responsible for modifying the query to achieve the desired filter state, while the options method defines the values the filter may have. Let's take a look at an example UserType filter:

<?php

namespace App\Lm\Filters;

use Illuminate\Http\Request;
use Anyspin\Lamotivo\Filters\Filter;

class UserType extends Filter
{
    /**
     * Apply the filter to the given query.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @param  mixed  $value
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function apply(Request $request, $query, $value)
    {
        return $query->where('type', $value);
    }

    /**
     * Get the filter's available options.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function options(Request $request)
    {
        return [
            'admin' => 'Administrator',
            'user' => 'User',
        ];
    }
}

The options method should return an array of keys and values. The values are labels, and the keys are values to use in the apply method as the $value argument.

The apply method should return the modified query instance.

Multi Filters

Lamotivo also supports multi filters, which allows the user to select multiple filter options.

You may generate a multi filter using the lm:filter --multi Artisan command:

php artisan lm:filter UserType --multi

Date Filters

Lamotivo also supports date filters, which allows the user to select the filter's value via a date selection calendar.

You may generate a date filter using the lm:filter --date Artisan command:

php artisan lm:filter BirthdayFilter --date

Each date filter generated by Lamotivo contains one method: apply.

When building date filters, the $value argument passed to the apply method is the string representation of the selected date range. The $value can be either a single date (e.g. 20200101) or a date range (e.g. 20200101-20200131)

Let's take a look at an example DateShipped filter:

<?php

namespace App\Lm\Filters;

use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Anyspin\Lamotivo\Filters\DateFilter as Filter;

class DateShipped extends Filter
{
    /**
     * Apply the filter to the given query.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @param  mixed  $value
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function apply(Request $request, $query, $value)
    {
        $dates = explode('-', $value);

        if (count($dates) === 1)
        {
            return $query->whereDate('shipped_at', $dates[0]);
        }
        else if (count($dates) === 2)
        {
            return $query->whereBetween('shipped_at', $dates);
        }
    }
}

Filter Title

If you would like to change the filter title that is displayed in Lamotivo's filter toolbar, you may define a title property on the filter class:

protected static $title = 'Date Shipped';

Registering Filters

Once you have defined a filter, you are ready to attach it to a resource. Each resource generated by Lamotivo contains a filters method. To attach a filter to a resource, you should simply add it to the array of filters returned by this method:

/**
 * Get the filters available for the resource.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
public function filters(Request $request)
{
    return [
        new Filters\DateShipped,
    ];
}