anthonyedmonds / laravel-find
以最小开销在您的Laravel系统中查找模型
Requires
- php: ^8.3
- illuminate/support: ^11
Requires (Dev)
- laravel/pint: ^1
- orchestra/testbench: ^9
- phpunit/phpunit: ^11
README
以最小开销在您的Laravel系统中查找模型
安装
使用以下命令添加此库: composer require anthonyedmonds/laravel-find
。
服务提供器将被自动注册。
使用以下命令导出配置文件: php artisan vendor:publish --provider="AnthonyEdmonds\LaravelFind\FindServiceProvider"
配置
在使用库之前,需要配置少量选项。
return [ /* The key and label for searching all models; set as false to disable */ 'anything-key' => 'any', 'anything-label' => 'Anything', /* A table that is guaranteed to exist */ 'base-table' => 'users', /* Which models can be searched for, and its display label */ 'models' => [ 'users' => \App\Models\User::class, ], ];
anything-key / anything-label
用户可以查找他们可以访问的任何模型。
使用这些键控制键和标签。
将键设置为false
以禁用对模型的搜索。
base-table
对所有模型进行搜索需要在其他所有模型上连接的基础查询。
将此值设置为系统中肯定存在的表的名称,例如'users'表。
models
此列表控制哪些模型可以使用此库查找。
要查找的每个模型类型应使用复数标签键入,模型类作为值。
用法
此库分为两部分,一个用于在要查找的模型上使用的Findable
特质,以及一个用于获取结果的Find
静态类。
Findable
在models
配置键中列出的每个模型都必须使用Findable
特质。这将添加一些必须实现的抽象静态方法。
class Author extends Model { use Findable; public static function findDisplayLabel(): string { return 'Authors by name and book title'; } public static function canBeFoundBy(?Model $user): bool { return true; } protected static function findLabel(): string { return 'name'; } protected static function findDescription(): string { return '"An author"'; } protected static function findLink(): string { return route('authors.show', '~id'); } protected static function findFilters(Builder $query, string $term): Builder { return $query->where('name', 'LIKE', "%$term%"); } }
findDisplayLabel
模型类型的显示标签,例如"按姓名和书名查找作者"。
主要用于识别模型,最好带有用户可以查找的术语的上下文。
canBeFoundBy
用于确定当前用户(如果未登录则为null)是否可以查找模型类型。
此逻辑将根据返回的true
或false
显示或隐藏整个模型类型。
如果用户尝试查找不允许的模型,Find辅助将抛出AuthorizationException
。
findLabel
结果的主要标识符,可以是
- 列名,例如'title'
- 静态描述,例如'"Book"'
- 带有占位符的句子,例如'~title by ~author'
findDescription
结果的简要描述,可以是
- 列名,例如'description'
- 静态描述,例如'"A collection of pages"'
- 带有占位符的句子,例如'~genre ~page_count'
findLink
可以找到结果的位置的URL,可以是
- 列名,例如'url'
- 静态链接,例如'"https://my-site.com/users""'
- 带有占位符的句子,例如'https://my-site.com/books/~book_id/~id'
findFilters
要应用于搜索的过滤器,例如->where('name', '=', $term)
。
由于此库使用Laravel的QueryBuilder,您可以通过执行匹配的leftJoin
和where
语句来使用相关模型查找结果
protected static function findFilters(Builder $query, string $term, ?Model $user = null): Builder { return $query ->leftJoin('books', 'books.id', '=', 'chapters.book_id') ->where('chapters.title', 'LIKE', "%$term%") ->orWhere('books.title', 'LIKE', "%$term%"); }
您可以使用$user
参数进一步细化搜索以针对当前用户。
Find
在为每个模型实现Findable
特质后,您可以使用Find
辅助来获取结果。
大多数实现需要为最终用户提供两个页面
- 开始
- 结果
开始页面应提供一个用于查找术语的文本框,以及一个下拉菜单来选择要查找的事物类型。
结果页面应显示找到的内容列表。
Find::findBy
调用 findBy($term, $type)
将构造并返回一个可执行的 QueryBuilder 语句。
$term
是查找过程中使用的值,传递给每个 Model 的findBy()
方法。$type
是models
配置中特定 Model 类型的键,或用于在所有允许的 Model 中查找的anything-key
。
作为一个标准查询,您可以按需扩展查询,例如添加一个 order()
。
使用所需的 QueryBuilder 方法运行查询,例如 paginate()
或 get()
。
$query = Find::findBy('my-term', 'users'); $query->orderBy('users.name'); $query->paginate(10);
这将返回一个包含结果的常规 Collection
对象。
每个结果将有一个 label
、description
和 link
属性。
Find::types
调用 types()
将返回当前用户可以查找的 Model 列表。
此列表可用于在开始查找时填充下拉菜单,以缩小要定位的 Model 类型,并识别用户被允许查找的 Model。
返回的数组使用配置中的 models
键进行键控。
默认情况下,值是每个 Model 的 findDisplayLabel()
。将 true
传递给该方法将返回 Model 类。
FindRequest
提供了一种方便的 Laravel FormRequest,称为 FindRequest
。
简单地在控制器方法上使用它来处理搜索
public function results(FindRequest $request): View { // Show results... }
路线图
- 路由宏?