PHP code example of travelbuild / eloquent-filterable

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

    

travelbuild / eloquent-filterable example snippets




namespace App;

use Travelbuild\Filterable\Filterable;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use Filterable;
}

/**
 * Scope a query to only y
 * @return Builder
 */
public function scopeActive(Builder $query): Builder
{
    return $query->where('active', true);
}

/**
 * The usable scope filters.
 *
 * @return array
 */
protected $filterable = [
    'active',
];

/**
 * The usable scope filters.
 *
 * @return array
 */
protected $filterable = ['*'];

$users = User::filter(['active' => true])->paginate();



namespace App\Http\Controllers;

use App\Post;
use Illuminate\Http\Request;

class PostSearchController extends Controller
{
    /**
     * Respond the post search request.
     *
     * @param  Request  $request
     * @return JsonResponse
     */
    public function __invoke(Request $request)
    {
        $posts = Post::filter($request->all())->paginate(
            $request->get('limit', 25)
        );

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

Route::get('/posts', 'PostSearchController');



namespace App;

use Travelbuild\Filterable\Filterable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;

class Post extends Model
{
    use Filterable;

    /**
     * The usable scope filters.
     *
     * @return array
     */
    protected $filterable = [
        'active',
        'author',
    ];

    /**
     * Scope a query to only lder $query, int $authorId): Builder
    {
        return $query->where('author_id', $authorId);
    }
}