PHP code example of kalimeromk / filterable

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

    

kalimeromk / filterable example snippets


// config/app.php
'providers' => [
    Kalimeromk\Filterable\FilterableServiceProvider::class,
];

use App\Models\User;

$users = User::query()
    ->whereLike(['name', 'email'], 'John')
    ->get();

$users = User::query()
    ->whereLike(['posts.title', 'posts.content'], 'Laravel')
    ->with('posts')
    ->get();

$users = User::query()
    ->whereLike(['posts.comments.body'], 'thanks')
    ->get();

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Kalimeromk\Filterable\Traits\Filterable;

class User extends Model
{
    use Filterable;

    protected $fillable = ['name', 'email', 'age', 'is_active'];

    // LIKE instead of exact match, for columns of this model
    protected $likeFields = ['name', 'email'];

    // cast to boolean before comparing
    protected $boolFields = ['is_active'];
}

use App\Models\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function index(Request $request)
    {
        $filters = $request->only(['name', 'email', 'is_active', 'age_min', 'age_max']);

        return response()->json(User::filter($filters)->get());
    }
}