PHP code example of buibr / eloquent-filter

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

    

buibr / eloquent-filter example snippets


use BI\EloquentFilter\HasFilters;

class YourModel extends Model
{
    use HasFilters;

    // Other model code...
}

use BI\EloquentFilter\QueryFilter;

class YourModelFilter extends QueryFilter
{
    public function status($value)
    {
        return $this->builder->where('status', $value);
    }

    public function category($value)
    {
        return $this->builder->where('category_id', $value);
    }

    // Add more filter methods as needed...
}

use App\Models\YourModel;
use App\Filters\YourModelFilter;

class YourController extends Controller
{
    public function index(YourModelFilter $filters)
    {
        $items = YourModel::filter($filters)->get();

        return response()->json($items);
    }
}

class YourModelFilter extends QueryFilter
{
    public function created_at($value)
    {
        return $this->builder->whereDate('created_at', $value);
    }

    public function sort($value)
    {
        return $this->builder->orderBy('created_at', $value);
    }
}

GET /your-models?status=active&category=1