PHP code example of laravie / query-filter

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

    

laravie / query-filter example snippets


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

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

$query = User::query();

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

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

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

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

$query = User::query();

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

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

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

$query = User::query();

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

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

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

$query = User::query();

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

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

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

$query = User::query();

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

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

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

$query = User::query();

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

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

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

new Laravie\QueryFilter\Taxonomy(?string $keyword, array $rules, array $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();



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



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