PHP code example of hackeresq / filter-models

1. Go to this page and download the library: Download hackeresq/filter-models 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/ */

    

hackeresq / filter-models example snippets


use HackerEsq\FilterModels\FilterModels;

class PortfolioController extends ApiController
{
    public function index(FilterModels $filters)
    {
        $filters->setQuery(Portfolio::query());
        $filters->setScopes(['myPortfolios']);
        $filters->setEagerRelations(['users', 'transactions', 'holdings']);
        $filters->setFilterableRelations(['holdings' => 'symbol', 'transactions' => 'symbol']);
        $filters->setSearchableColumns(['title', 'notes']);

        return $filters->paginated();
    }
}

namespace App\Filters;

use HackerEsq\FilterModels\FilterModels;

class PortfolioFilter extends FilterModels
{
    public function apply(): void
    {
        $this->setModel(Portfolio::class);
        $this->setScopes(['myPortfolios']);
        $this->setEagerRelations(['users', 'transactions', 'holdings']);
        $this->setFilterableRelations(['holdings' => 'symbol', 'transactions' => 'symbol']);
        $this->setSearchableColumns(['title', 'notes']);
    }
}

use App\Filters\PortfolioFilters;

class PortfolioController extends ApiController
{
    public function index(PortfolioFilters $filters)
    {

        return $filters->paginated();
    }
}