hanieas/larasearch 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

hanieas/larasearch

最新稳定版本:0.0.1

Composer 安装命令:

composer require hanieas/larasearch

包简介

README 文档

README

This is a search package built to easily integrate with Laravel and the database.

Installation

composer require hanieas/larasearch

Usage

Prepare Model

In order to search through models you'll have to let them implement the Searchable interface.

namespace LaraSearch;

interface Searchable
{
    /**
     * @return array
     */
    public function getColumnsForExactMatch(): array;

    /**
     * @return array
     */
    public function getColumnsForLikeMatch(): array;

    /**
     * @return array
     */
    public function getColumnsForBooleanMatch(): array;

    /**
     * @return array
     */
    public function getColumnsForPeriodMatch(): array;
}

You need to add the below 4 functions in your model to define type of search for columns of your table. This package will search items that are in the fillable attribute of the model.

use LaraSearch\Searchable;

class User extends Model implements Searchable
{
    protected $fillable = [
        'username',
        'mobile',
        'is_active',
        'logged_in',
    ];
     
    /**
     * @return array
     */
    public function getColumnsForExactMatch(): array {
        return [  
            'mobile'
        ];
    }

    /**
     * @return array
     */
    public function getColumnsForLikeMatch(): array {
         return [  
            'username'
        ];
    }

    /**
     * @return array
     */
    public function getColumnsForBooleanMatch(): array {
         return [  
            'is_active'
        ];
    }

    /**
     * @return array
     */
    public function getColumnsForPeriodMatch(): array
    {
        return [  
            'logged_in'
        ];
    }
}

Search

With the models prepared you can search them like this:

class UserController extends Controller
{

    /**
     * @return JsonResponse
     */
    public function index(): JsonResponse
    {
        $users = new Search(Request()->toArray(), User::query())->get();
        return response()->json([
            'users' => $users
        ]);
    }
}

You can easily search from the users' list by calling the below link:

{{address}}/users?username=hanie?mobile=?is_active=1?logged_in_from=2023-10-10?logged_in_to=2023-12-10

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2023-11-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固