Looking to hire Laravel developers? Try LaraJobs

laravel-global-search maintained by php-junior

Description
Search Package For Laravel
Author
Last update
2018/09/26 11:27 (dev-master)
License
Links
Downloads
623
Tags

Comments
comments powered by Disqus

Laravel Global Search [WIP]

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.