PHP code example of kamil-koscielniak / eloquent-filters

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

    

kamil-koscielniak / eloquent-filters example snippets




namespace App;

use Illuminate\Database\Eloquent\Model;
use KamilKoscielniak\EloquentFilters\Filters\PartialFilter;
use KamilKoscielniak\EloquentFilters\Filters\RangeFilter;
use KamilKoscielniak\EloquentFilters\Traits\Filterable;

class Product extends Model
{
    use Filterable;
    
    public static array $filters = [
        'code' => PartialFilter::class,
        'price' => RangeFilter::class,
    ];
}



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Product;

class ProductController extends Controller
{
    public function index(Request $request)
    {
        $products = Product::filter($request)->get();

        return response()->json(compact('products'));
    }
}



namespace App;

use Illuminate\Database\Eloquent\Model;
use KamilKoscielniak\EloquentFilters\Filters\PartialFilter;
use KamilKoscielniak\EloquentFilters\Traits\Filterable;

class Product extends Model
{
    use Filterable;
    
    public static array $filters = [
        'category__code' => PartialFilter::class,
    ];

    public function category()
    {
        return $this->belongsTo(Category::class);
    }

}
bash
php artisan vendor:publish --tag=config