PHP code example of petrelli / search-interface-builder

1. Go to this page and download the library: Download petrelli/search-interface-builder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

petrelli / search-interface-builder example snippets


@foreach ($filtering->filters() as $filter)
    <div>
        <h4>{{ $filter->label() }}</h4>
        @foreach($filter->links() as $option)
            <a href="{{$option->url}}" @if($option->active) class="active" @endif>
                {{ $option->label }}
            </a>
        @endforeach
    </div>
@endforeach

@if ($filtering->activeFilters()->isNotEmpty())
    <div>
        <h4>Selected filters:</h4>
        @foreach($filtering->activeFilters() as $option)
            <a href="{{$option->url}}">{{ $option->label }}</a>
        @endforeach
        <a href="{!! route('filters') !!}">Clear all</a>
    </div>
@endif

'providers' => [
    //...
    Petrelli\SearchInterfaceBuilder\ServiceProvider::class,
    //...
]

namespace App\Filters\Definitions;

class Location extends Petrelli\SearchInterfaceBuilder\MultipleSelector
{

    protected $parameter = 'filter_location';

    protected $label     = 'Location';


    public function values()
    {

        //... Always return a [value => label ...] associative array
        return [
            'ny' => __('New York'),
            'nb' => __('Nairobi'),
            'fr' => __('France'),
        ];

    }

}


// Filters
use App\Filters\Definitions\Department;
use App\Filters\Definitions\Location;

// Sorter
use App\Filters\Definitions\SortStaff;


class StaffFiltering extends Petrelli\SearchInterfaceBuilder\BaseSection
{

    protected $route = 'staff';

    protected $filters = [
        Department::class,
        Location::class,
    ];

    protected $sorter = SortStaff::class;

}


return view('staff.index', [
    'filtering' => app(StaffsFiltering::class),
    // ...
]);


@foreach ($filtering->filters() as $filter)
  <span>{{ $filter->label() }}</span>
@endforeach

@foreach ($filtering->filters() as $filter)
    <span>{{ $filter->label() }}</span>

    @foreach($filter->links() as $option)
        <a href="{{$option->url}}" class="@if($option->active) is-active @endif">
            {{ $option->label }}
        </a>
    @endforeach

@endforeach

$option->label   // Label
$option->value   // Value
$option->active  // Boolean that indicates if is active
$option->urlRoot // URL with no filters at all
$option->url     // URL that contains or not the value depending if it's active (url-present) or not

@if ($filters->activeFilters()->isNotEmpty())
    @foreach($filters->activeFilters() as $filter)
      <a href="{{$filter->url}}">{{ $filter->label }}</a>
    @endforeach

    <a href="{!! route('staff') !!}">Clear all</a>
  </div>
@endif

class Location extends Petrelli\SearchInterfaceBuilder\SingleSelector

public function buildRoute($extraParams)
{

    return route($this->route, request()->except(['page', $this->parameter]) + $extraParams);

}