Giter Club home page Giter Club logo

query-filter's Introduction

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);
    }
}

query-filter's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

query-filter's Issues

search doesn't work if search term is not english

  • Package Version: v1.6.1
  • Laravel Version: 7.20.0
  • PHP Version: PHP 7.4.7 (cli) (built: Jun 12 2020 00:00:24) ( NTS )
  • Database Driver & Version: mysql v8.0.19

Description:

if search keyword is not english, the search query wont be applied

Steps To Reproduce:

  • add a some records with different english text and one record with a something else ex. in arabic ุงู„ุฌู†ุฉ
  • try to search new Searchable('ุงู„ุฌู†ุฉ', ['column_name']);
  • nothing will come out

Column 'xxx' in where clause is ambiguous

  • Package Version: v1.6.2
  • Laravel Version: 7.20.0
  • PHP Version: PHP 7.4.7 (cli) (built: Jun 12 2020 00:00:24) ( NTS )
  • Database Driver & Version: mysql v8.0.19

Description:

sometimes when searching in related relations we would get Column 'xxx' in where clause is ambiguous , the solution for it is to use

$builder->qualifyColumn('column_name')

on each relation to make sure the column is scoped correctly to its table, but how/where to put that logic ?

Steps To Reproduce:

it happens from time to time, on simple & complex relations.

  • an example
SELECT
	*
FROM
	`accounts`
WHERE (EXISTS (
		SELECT
			*
		FROM
			`regions`
			INNER JOIN `clients` ON `clients`.`region_id` = `regions`.`id`
		WHERE
			`accounts`.`client_id` = `clients`.`id`
			and(`name` LIKE 'test'
				OR `name` LIKE 'test%'
				OR `name` LIKE '%test'
				OR `name` LIKE '%test%')
		ORDER BY
			`regions`.`created_at` DESC))
ORDER BY
	`accounts`.`created_at` DESC
  • error
Query 1 ERROR: Column 'name' in where clause is ambiguous

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.