Eloquent & Datenzugriff v2

Prepopulation Trait

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

Version und Seitenübersicht

Sie lesen Version v2. Die aktuelle Dokumentation ist v4.

Commit 47f72d9227c5 Diese Seite auf GitHub

The Prepopulation trait extends the functionality of a SelectFilter. Like the EnumFilter that automatically offers all cases out of an PHP Enum, this Prepopulation trait automatically loads all select options out of the database.

Given you have a field of vendor names and the name is saved in a database field named 'vendor', and you need a filter that offers all existing vendors in a select box, you can just extend your SelectFilter with the Prepopulation trait.

<?php

namespace App\Models\Filters;

use Lacodix\LaravelModelFilter\Filters\SelectFilter;
use Lacodix\LaravelModelFilter\Filters\Traits\Prepopulation;

class PrepopulatedSelectFilter extends SelectFilter
{
    use Prepopulation;
    
    protected string $field = 'vendor';
}

If you need to map the values, maybe because of translations, you can just add the mapOption method.

protected function mapOption(string $option): string
{
    return __('translations.' . $option);
}

Technisches Erstgespräch

Aus einem Package wird noch kein belastbares Produkt.

Wenn Architektur, Integration oder langfristiger Betrieb entscheidend sind, sprechen wir gern über den vollständigen Anwendungskontext.

Projekt besprechen