All Projects → laravie → query-filter

laravie / query-filter

Licence: MIT license
🔍 Database/Eloquent Query Builder filters for Laravel

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to query-filter

searchable
Pattern-matching search and reusable queries in laravel.
Stars: ✭ 28 (-59.42%)
Mutual labels:  query-builder, searchable, eloquent-search
laravel-arangodb
ArangoDB driver for Laravel
Stars: ✭ 43 (-37.68%)
Mutual labels:  eloquent, query-builder
Pecee Pixie
Lightweight, easy-to-use querybuilder for PHP inspired by Laravel Eloquent - but with less overhead.
Stars: ✭ 19 (-72.46%)
Mutual labels:  eloquent, query-builder
laravel-query-inspector
The missing laravel helper that allows you to inspect your eloquent queries with it's bind parameters
Stars: ✭ 59 (-14.49%)
Mutual labels:  eloquent, query-builder
Datatable
Modular server side Datatable package for Laravel 5 for various client side table plugins
Stars: ✭ 52 (-24.64%)
Mutual labels:  eloquent, query-builder
Eloquent Builder
Provides an advanced filter for Laravel or Lumen model.
Stars: ✭ 264 (+282.61%)
Mutual labels:  eloquent, query-builder
Elasticsearch
The missing elasticsearch ORM for Laravel, Lumen and Native php applications
Stars: ✭ 375 (+443.48%)
Mutual labels:  eloquent, query-builder
Laravel Eloquent Query Cache
Adding cache on your Laravel Eloquent queries' results is now a breeze.
Stars: ✭ 529 (+666.67%)
Mutual labels:  eloquent, query-builder
Querybuilderparser
A simple to use query builder for the jQuery QueryBuilder plugin for use with Laravel.
Stars: ✭ 126 (+82.61%)
Mutual labels:  eloquent, query-builder
acorn-db
Provides Acorn projects with Eloquent Models for WordPress data.
Stars: ✭ 30 (-56.52%)
Mutual labels:  eloquent
php-orm-benchmark
The benchmark to compare performance of PHP ORM solutions.
Stars: ✭ 82 (+18.84%)
Mutual labels:  eloquent
eloquent-filter
Library to form search criteria through expressions in the query string
Stars: ✭ 23 (-66.67%)
Mutual labels:  eloquent
state-machine
The hyn state machine package is a flexible library that helps you move Eloquent models from States through Transitions while emitting events along the way.
Stars: ✭ 14 (-79.71%)
Mutual labels:  eloquent
sea-query
🔱 A dynamic SQL query builder for MySQL, Postgres and SQLite
Stars: ✭ 595 (+762.32%)
Mutual labels:  query-builder
Gridify
Easy and optimized way to apply Filtering, Sorting, and Pagination using text-based data.
Stars: ✭ 372 (+439.13%)
Mutual labels:  query-builder
sparklis
Sparklis is a query builder in natural language that allows people to explore and query SPARQL endpoints with all the power of SPARQL and without any knowledge of SPARQL.
Stars: ✭ 28 (-59.42%)
Mutual labels:  query-builder
flepper
Flepper is a library to aid in database interaction. 🐸
Stars: ✭ 60 (-13.04%)
Mutual labels:  query-builder
laravel-scoped-cache
Easily cache items specific to your Eloquent models without having to append the ID.
Stars: ✭ 28 (-59.42%)
Mutual labels:  eloquent
eloquent-hashids
Automatically generate and persist Hashids for newly created Eloquent models.
Stars: ✭ 17 (-75.36%)
Mutual labels:  eloquent
laravel-cachable-attributes
Allows to cache attribute accessor values in an easy way.
Stars: ✭ 24 (-65.22%)
Mutual labels:  eloquent

Database/Eloquent Query Builder filters for Laravel

tests Latest Stable Version Total Downloads Latest Unstable Version License Coverage Status

Installation

To install through composer, run the following command from terminal:

composer require "laravie/query-filter"

Usages

Order Queries

new Laravie\QueryFilter\Orderable(?string $column, string $direction = 'asc', array $config = []);

The class provides a simple interface to handle ORDER BY queries to Laravel Eloquent/Query Builder.

use App\User;
use Laravie\QueryFilter\Orderable;

$query = User::query();

$orderable = new Orderable(
    'name', 'desc'
);

return $orderable->apply($query)->get(); 
select * from `users` order by `name` desc;

The code will validate the column name before trying to apply orderBy() to the query, this would prevent SQL injection especially when column is given by the user.

Search Queries

new Laravie\QueryFilter\Searchable(?string $keyword, array $columns = []);

The class provides a simple interface to LIKE queries to Laravel Eloquent/Query Builder.

use App\User;
use Laravie\QueryFilter\Searchable;

$query = User::query();

$searchable = new Searchable(
    'crynobone', ['name', 'email']
);

return $searchable->apply($query)->get(); 
select * from `users` 
where (
    (
        `name` like 'crynobone' 
        or `name` like 'crynobone%'
        or `name` like '%crynobone'
        or `name` like '%crynobone%'
    ) or (
        `email` like 'crynobone' 
        or `email` like 'crynobone%'
        or `email` like '%crynobone'
        or `email` like '%crynobone%'
    )
);

Search with wildcard

Set specific % or * wildcard to reduce the possible LIKEs variations.

use App\User;
use Laravie\QueryFilter\Searchable;

$query = User::query();

$searchable = new Searchable(
    'crynobone*gmail', ['name', 'email']
);

return $searchable->apply($query)->get(); 
select * from `users` 
where (
    (
        `name` like 'crynobone%gmail'
    ) or (
        `email` like 'crynobone%gmail'
    )
);

Search with exact wildcard

Use noWildcardSearching() to disable adding additional search condition.

use App\User;
use Laravie\QueryFilter\Searchable;

$query = User::query();

$searchable = (new Searchable(
    'crynobone@gmail', ['name', 'email']
))->noWildcardSearching();

return $searchable->apply($query)->get(); 
select * from `users` 
where (
    (
        `name` like 'crynobone@gmail'
    ) or (
        `email` like 'crynobone@gmail'
    )
);

Search with JSON path

This would allow you to query JSON path using LIKE with case insensitive.

use App\User;
use Laravie\QueryFilter\Searchable;

$query = User::query();

$searchable = new Searchable(
    'Malaysia', ['address->country']
);

return $searchable->apply($query)->get(); 
select * from `users` 
where (
    (
        lower(json_unquote(json_extract(`meta`, '$."country"'))) like 'malaysia'
        or lower(json_unquote(json_extract(`meta`, '$."country"'))) like 'malaysia%'
        or lower(json_unquote(json_extract(`meta`, '$."country"'))) like '%malaysia'
        or lower(json_unquote(json_extract(`meta`, '$."country"'))) like '%malaysia%'
    )
);

Search with Relations

This would make it easy to search results not only in the current model but also it's relations.

use App\User;
use Laravie\QueryFilter\Searchable;

$query = User::query();

$searchable = new Searchable(
    'Administrator', ['name', 'roles.name']
);

return $searchable->apply($query)->get(); 
select * from `users` 
where (
    (
        `name` like 'Administrator' 
        or `name` like 'Administrator%'
        or `name` like '%Administrator'
        or `name` like '%Administrator%'
    ) or exists (
        select * from `roles` 
        inner join `user_role` 
            on `roles`.`id` = `user_role`.`role_id` 
        where `users`.`id` = `user_role`.`user_id` 
        and (
            `name` like 'Administrator' 
            or `name` like 'Administrator%' 
            or `name` like '%Administrator' 
            or `name` like '%Administrator%'
        )
    )
);

Relations search can only be applied to Illuminate\Database\Eloquent\Builder as it need to ensure that the relationship exists via whereHas() queries.

Search with Morph Relations

You can use polymorphic relationship search using the following options:

use App\Comment;
use Laravie\QueryFilter\Searchable;
use Laravie\QueryFilter\Filters\MorphRelationSearch;

$query = Comment::query();

$searchable = new Searchable(
    'Administrator', ['name', new MorphRelationSearch('commentable', 'name')]
);

return $searchable->apply($query)->get(); 

Taxonomy Queries

new Laravie\QueryFilter\Taxonomy(?string $keyword, array $rules, array $columns = []);

Taxonomy always developers to create a set of rules to group the search keywords using WHERE ... AND. For any un-grouped keyword it will be executed via Laravie\QueryFilter\Searchable based on given $columns.

use App\User;
use Laravie\QueryFilter\Taxonomy;

$query = User::query();

$taxonomy = new Taxonomy(
    'is:admin email:[email protected]', [
        'email:*' => static function ($query, $value) {
            return $query->where('email', '=', $value);
        },
        'role:[]' => static function ($query, array $value) {
            return $query->whereIn('role', $value);
        },
        'is:admin' => static function ($query) {
            return $query->where('admin', '=', 1);
        },
    ],
);

$taxonomy->apply($query)->get();
select * from `user` 
where `email`='[email protected]'
and `admin`=1;

Integrations

Query Builder Macro

You can integrate Searchable with database or eloquent query builder macro by adding the following code to your AppServiceProvider (under register method):

<?php

namespace App\Providers;

use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Support\Arr;
use Laravie\QueryFilter\Searchable;

class AppServiceProvider extends \Illuminate\Support\ServiceProvider 
{
    /**
     * Register any application services.
     *
     * This service provider is a great spot to register your various container
     * bindings with the application. As you can see, we are registering our
     * "Registrar" implementation here. You can add your own bindings too!
     *
     * @return void
     */
    public function register()
    {
        QueryBuilder::macro('whereLike', static function ($attributes, string $searchTerm) {
            return (new Searchable($searchTerm, Arr::wrap($attributes)))->apply($this);
        });

        EloquentBuilder::macro('whereLike', static function ($attributes, string $searchTerm) {
            return (new Searchable($searchTerm, Arr::wrap($attributes)))->apply($this);
        });
    }
}

Using with Laravel Nova

You can override the default Laravel global and local search feature by adding the following methods on app/Nova/Resource.php:

<?php

namespace App\Nova;

use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Resource as NovaResource;
use Laravie\QueryFilter\Searchable;

abstract class Resource extends NovaResource
{
    // ...
    
    /**
     * Apply the search query to the query.
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @param string                                $search
     *
     * @return \Illuminate\Database\Eloquent\Builder
     */
    protected static function applySearch($query, $search)
    {
        $searchColumns = static::searchableColumns() ?? [];

        return static::initializeSearch($search, $searchColumns)->apply($query);
    }

    /**
     * Initialize Search.
     *
     * @param  string  $search
     * @param  array  $searchColumns
     * @return \Laravie\QueryFilter\Searchable
     */
    protected static function initializeSearch($search, $searchColumns)
    {
        return new Searchable($search, $searchColumns);
    }
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].