Eloquent & Datenzugriff v3

Filter for Relations

Laravel Model Filter · A Laravel package for filtering and searching models from database with ease.

Version und Seiten

Sie lesen Version v3. Die aktuelle Dokumentation ist v4.

Commit 4616a40d07f1 Diese Seite auf GitHub

You can create a simple Relation filter by extending the base SelectFilter. In the following example we create a Tag-Filter that can be applied to all models that have a tag relation.

Thanks to hfmiguel for this example.

class TagsFilter extends SelectFilter
{
    public FilterMode $mode = FilterMode::EQUAL;

    protected string $field = 'tag_id';  /**  column of the relation */ 
    protected string $relation = 'tags';  /** relation name : where the search will be applied */

    public function title(): string
    {
        return __('Tags');
    }

    public function options(): array
    {
        return Tag::query()
            ->orderBy('name')
            ->pluck('id', 'name')
            ->toArray();
    }
    
    public function apply(Builder $query): Builder
    {
        return $query->whereHas($this->relation, function (Builder $query) {
            $query->whereIn($this->field, $this->values);
        });
    }
}

Technisches Erstgespräch

Ein Paket als Baustein für Ihre Anwendung.

Wenn Architektur, Integration oder Betrieb entscheidend sind, besprechen wir Ihre Anwendung als Ganzes.

Projekt besprechen