makidizajnerica/laravel-searcher
Composer 安装命令:
composer require makidizajnerica/laravel-searcher
包简介
Simple model and multi-model search.
README 文档
README
Simple model and multi-model search.
Installation
composer makidizajnerica/laravel-searcher
Model Preparation
Model should implement MakiDizajnerica\Searcher\Contracts\Searchable interface, next define attributesForTags() method inside of it.
This method should return attribute values that will be used as model tags.
After that add MakiDizajnerica\Searcher\Searchable trait.
<?php use Illuminate\Database\Eloquent\Model; use MakiDizajnerica\Searcher\Searchable; use MakiDizajnerica\Searcher\Contracts\Searchable as SearchableContract; class Post extends Model implements SearchableContract { use Searchable; /** * Get model attributes that will be used for tags. * * @return array<int, string> */ public function attributesForTags(): array { return $this->only(['title']); } }
Tags will be automaticly created on model creation, updated on model updation, and deleted when model is deleted.
If you want to create model without tags you should use createWithoutTags() method:
<?php use App\Models\Post; $post = Post::createWithoutTags(['title' => 'Test']);
You can also update model without touching its tags:
<?php use App\Models\Post; $post = Post::first(); // Example 1 $post->updateWithoutTags(['title' => 'New Test']); // Example 2 $post->title = 'New Test'; $post->saveWithoutTags();
When deleting model, you can also disable tags deletion:
<?php use App\Models\Post; $post = Post::first(); $post->withoutTags()->delete();
Models by default will be grouped by their table name, if you want to change that you can define $searchType property on the model:
<?php use Illuminate\Database\Eloquent\Model; use MakiDizajnerica\Searcher\Searchable; use MakiDizajnerica\Searcher\Contracts\Searchable as SearchableContract; class Post extends Model implements SearchableContract { use Searchable; /** * @var string */ protected $searchType = 'news'; }
Usage
Single Model Search
When searching single model, you can just pass query string to whereTags() scope.
<?php use App\Models\Post; class PostController extends Controller { public function index(Request $request) { return view('post.index', [ 'posts' => Post::whereTags($request->query('search'))->paginate() ]); } }
Multiple Model Search
<?php use App\Models\User; use App\Models\Post; use MakiDizajnerica\Searcher\Search; use Illuminate\Database\Eloquent\Builder; class PostController extends Controller { public function index(Request $request) { return view('post.index', [ 'search' => (new Search) ->addModel(User::class, function (Builder $query) { $query->active()->notAdministrator(); }) ->addModel(Post::class, ['latest', 'limit' => 10]) ->search($request->query('search')) ]); } }
Method's addModel() first parametar is class name of the model that will be searched. The second parametar is the scope, you can pass Closure and array like the example above.
You can also pass string which will represent the scope method name:
<?php use App\Models\Post; use MakiDizajnerica\Searcher\Search; (new Search)->addModel(Post::class, 'latest')->search('test', true);
You may also pass second bool parametar to the search() method, if you want strict searcing. By default its set to false, which means that tags will be search in LIKE clause.
When strict search is set to true, only the models with exact tags will be found.
Return value of the search() method will be MakiDizajnerica\Searcher\Collections\SearchResultCollection instance.
When rendering the search results, you can do something like this:
@foreach($search as $type => $results)
<p>
{{ $type }}
</p>
@foreach($results as $result)
<div>
<h1>
{{ $result->title }}
</h1>
</div>
@endforeach
@endforeach
Property $results will represent array of models that you can loop through.
Author
Nemanja Marijanovic (n.marijanovic@hotmail.com)
Licence
Copyright © 2021, Nemanja Marijanovic n.marijanovic@hotmail.com
All rights reserved.
For the full copyright and license information, please view the LICENSE file that was distributed within the source root of this package.
makidizajnerica/laravel-searcher 适用场景与选型建议
makidizajnerica/laravel-searcher 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 47 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 03 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「framework」 「php」 「search」 「model」 「laravel」 「models」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 makidizajnerica/laravel-searcher 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 makidizajnerica/laravel-searcher 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 makidizajnerica/laravel-searcher 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
A Laravel package to retrieve data from Google Search Console
Indexed Search Autocomplete - Extends the TYPO3 Core Extension Indexed_Search searchform with an autocomplete feature.
Abstraction Layer to index and search entities
Pimcore 10.x Website Indexer (powered by Zend Search Lucene)
Symfony bundle for Elasticsearch integration with round-robin load balancing
统计信息
- 总下载量: 47
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-03-04