assemble/eloquentsearch

根据模型关系中的复杂搜索条件搜索和检索优雅模型实体。

1.5.1 2019-02-26 16:33 UTC

This package is auto-updated.

Last update: 2024-08-28 23:17:35 UTC


README

安装

将此行添加到您的 providers 数组中

Assemble\EloquentSearch\EloquentSearchServiceProvider::class,

将此行添加到您的 aliases 数组中

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

您需要运行 php artisan vendor:publish 来将配置文件发布到您的安装目录,运行后,您可以在 config/eloquenet_search.php 中找到它。此配置文件用于控制哪些模型用于搜索/返回实体。

配置

配置文件可以在Laravel的配置目录中找到,位于 config/eloquent_search.php,在这里您可以定义与您的模型相关的类,如下所示。

return [
	'search_models' => [
		/* 
			Add your searchable eloquent models here, this is an example of user model usage.

			If you have your models sitting in the app root as default, something like this should find them.
			
				'user' => User::class,
	    	
	    	Otherwise if you have them elsewhere or the application cant seem to find them, try prefix them as such.
	    
	    		'user' => App\Models\User::class,

	    */
	   	
	    'user' => User::class,

	]
];

用法

为了使用搜索功能,您需要在模型上实现一个 $searchable 属性,以详细说明哪些字段和关系是可搜索的。

/*
*   Searchable Fields
*/
public $searchable = [
    'name', 'user_id', // fields
    'user', 'tags', // relations
];

您还可以在模型中实现 'isSearchable' 方法,以便搜索器确定是否允许搜索/返回该模型。

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

此功能允许您限制用户搜索只限于他们有权查看的模型。