php-junior/laravel-global-search

Search Package For Laravel

v0.0.1 2018-09-26 03:26 UTC

This package is auto-updated.

Last update: 2024-11-26 22:48:53 UTC


README

Laravel Global Search

Latest Stable Version Total Downloads License

Installation

composer require php-junior/laravel-global-search

Laravel 5.5 uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider.

If you don't use auto-discovery, add the ServiceProvider to the providers array in config/app.php

PhpJunior\LaravelGlobalSearch\LaravelGlobalSearchProvider::class,
php artisan vendor:publish --provider="PhpJunior\LaravelGlobalSearch\LaravelGlobalSearchProvider"

This is the contents of the published config file:

return [
    'resources' => [
        \App\Models\Auth\User::class
    ],
    'limit' => 10
];

Usage

First PhpJunior\LaravelGlobalSearch\Traits\GlobalSearchable trait to models

use PhpJunior\LaravelGlobalSearch\Traits\GlobalSearchable;

class User extends Authenticatable
{
    use GlobalSearchable;
    
    /**
     * The columns that should be searched.
     *
     * @var array
     */
    protected  $search = [
        'name', 'email',
    ];

    /**
     * The columns that should be displayed.
     *
     * @var array
     */
    protected $only = [
        'name', 'email'
    ];

    /**
     * The columns that should be ordered.
     *
     * @var array
     */
    protected  $order = [
        'name' => 'desc',
        'email' => 'asc'
    ];

    // Optional
    protected $searchQuery = [
        [
            'method' => 'where',
            'column' => 'email',
            'operator' => '=',
            'value' => 'usern@user.com'
        ],
        [
            'method' => 'whereBetween',
            'column' => 'votes',
            'value' => [1, 100]
        ]
    ];

    /**
     * @var string
     */
    protected $searchIndex = 'users-index';
}

Search

LaravelGlobalSearch::search($text)

Credits

  • All Contributors

License

The MIT License (MIT). Please see License File for more information.