Easy search with Eloquent

Submitted by wallacemaxters - 1 month ago

Easy way to create many filters aggregated in Eloquent with query string

public function index(Request $request)
{

    $search = function ($query) use($request) {
        foreach ($request->only('title', 'content', 'description') as $name => $value) {
            strlen($value) && $query->where($name, 'LIKE', "%{$value}%");
        }
    };

    return Post::where($search)->paginate();
}