PHP code example of alvariumsoft / laravel-filters

1. Go to this page and download the library: Download alvariumsoft/laravel-filters 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/ */

    

alvariumsoft / laravel-filters example snippets


Alvarium\Filters\FiltersServiceProvider::class,

'state' => \App\Alvarium\Filters\Services\Data\Defaults\DefaultState::class,

Route::get('/catalog/{category}/{subcategory}/filter/{param1?}/{param2?}', 'CategoryController@subcategory')->name('subcategory');

    // Класс фильтров регистрируется в сервис контейнере в сервис провайдере, поэтому мы можем сделать инъекцию прямо в контроллере.
    public function subcategory(Filters $filters, Request $request, Category $category, Category $subcategory, ...$params)
    {
        $categories = Category::with('categories')->whereNull('category_id')->get();

        // Получаем менеджер фильтров, передаем параметры в сегментах, все GET-параметры, название маршрута, параметры не являющиеся фильтрами
        $filterManager = $filters->getManager($params, $request->all(), 'subcategory', [$category, $subcategory]);

        // Получаем массив фильтров
        $filters = $filterManager->getFilters();
        // Получаем массив выбрвнных фильтров
        $chosenFilters = $filterManager->getChosenFilters($filters);

        // Получаем ид отфильтрованных товаров
        $productIds = $filterManager->getProductIds(Product::where('category_id', $subcategory->id));

        $products = Product::with('category')->whereIn('id', $productIds)->paginate(10);

        return view('catalog', compact('categories', 'products', 'filters', 'chosenFilters'));
    }

        'state' => \App\Alvarium\Filters\Services\Data\Defaults\DefaultState::class,

        'raw_data' => \App\Alvarium\Filters\Services\Data\Defaults\DefaultRawData::class,

        'receive_filter' => \App\Alvarium\Filters\Services\Filters\Defaults\DefaultReceiveFilter::class,

        'filters_creator' => \App\Alvarium\Filters\Services\Filters\Defaults\DefaultFiltersCreator::class,

        'links_creator' => \App\Alvarium\Filters\Services\Links\Defaults\DefaultLinksCreator::class,

    'middleware' => [
        \App\Alvarium\Filters\Services\Middleware\Defaults\SortFilters::class,
    ],

        'params_strategy' => \App\Alvarium\Filters\Services\Params\Defaults\DefaultParamsStrategy::class,

        'query_decorator' => \App\Alvarium\Filters\Services\Queries\Defaults\DefaultQueryDecorator::class,

php artisan vendor:publish --provider="Alvarium\Filters\FiltersServiceProvider"