承接 makidizajnerica/laravel-searcher 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 makidizajnerica/laravel-searcher 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 47
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-03-04