项目作者: AssembleOnline

项目描述 :
Searching facility to recursively search relations defined in Laravel5 Eloquent ORM
高级语言: PHP
项目地址: git://github.com/AssembleOnline/eloquentsearch.git
创建时间: 2016-02-24T09:48:44Z
项目社区:https://github.com/AssembleOnline/eloquentsearch

开源协议:

下载


Eloquent Model Searcher for Laravel 5

Installation

Add this line to your providers array:

  1. Assemble\EloquentSearch\EloquentSearchServiceProvider::class,

Add this line to your aliases array:

  1. 'EloquentSearch' => Assemble\EloquentSearch\Facades\EloquentSearcher::class,

You will need to run php artisan vendor:publish to publish the config file to your instalation,
Once run, you can find it in config/eloquenet_search.php.
This config file is used to controll which models are used to search/return entities of.

Configuration

The config file can be found in the laravel config folder at config/eloquent_search.php,
here you can define the classes related to your models as below.

  1. return [
  2. 'search_models' => [
  3. /*
  4. Add your searchable eloquent models here, this is an example of user model usage.
  5. If you have your models sitting in the app root as default, something like this should find them.
  6. 'user' => User::class,
  7. Otherwise if you have them elsewhere or the application cant seem to find them, try prefix them as such.
  8. 'user' => App\Models\User::class,
  9. */
  10. 'user' => User::class,
  11. ]
  12. ];

Usage

To make use of the search functionality, you will need to implement a $searchable property on your models to detail which fields and relations are searchable.

  1. /*
  2. * Searchable Fields
  3. */
  4. public $searchable = [
  5. 'name', 'user_id', // fields
  6. 'user', 'tags', // relations
  7. ];

You can also implement the method ‘isSearchable’ in your models for the searcher to determine if it is allowed to search/return that model.

  1. public function isSearchable(){
  2. // Do your checks to determine if the model may be searched by the user
  3. return true;
  4. }

This feature lets you restrict user searches to only the models they are allowed to see.