php-junior/laravel-global-search

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

This package is auto-updated.

Last update: 2024-09-26 22:28:49 UTC


README

Laravel全局搜索

Latest Stable Version Total Downloads License

安装

composer require php-junior/laravel-global-search

Laravel 5.5使用包自动发现,因此不需要您手动添加ServiceProvider。

如果您不使用自动发现,请将ServiceProvider添加到config/app.php中的providers数组

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

这是已发布配置文件的内容

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

用法

首先将PhpJunior\LaravelGlobalSearch\Traits\GlobalSearchable特性添加到模型中

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

搜索

LaravelGlobalSearch::search($text)

致谢

  • 所有贡献者

许可证

MIT许可证(MIT)。请参阅许可证文件以获取更多信息。