定制 vluzrmos/eloquent-simple-searchable 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

vluzrmos/eloquent-simple-searchable

最新稳定版本:v0.1.1

Composer 安装命令:

composer require vluzrmos/eloquent-simple-searchable

包简介

An Eloquent Simple Searchable Scope.

README 文档

README

Latest Stable Version Total Downloads Latest Unstable Version License

Installation

composer require vluzrmos/eloquent-simple-searchable

Usage

Put that trait into your model:

namespace App;

use Illuminate\Database\Eloquent\Model;
use Vluzrmos\SimpleSearchable\Eloquent\SimpleSearchableTrait;

class User extends Model
{ 
	use SimpleSearchableTrait;

	protected $searchable = [
		'field' => 'type'
	];
}

The attribute $searchable should contain the index with a column or a related column and the value is a type of the search which includes:

  • left_text: Match the left side of the column value
  • right_text: Match the right side of the column value
  • equals: The searchd text should be equals to the column value
  • full_text: The searched text should be in any position of the searchd column.

Real Life Usage

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

use Vluzrmos\SimpleSearchable\Eloquent\SimpleSearchableTrait;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
	use Authenticatable, CanResetPassword, SimpleSearchableTrait;

	protected $searchable = [
		'name' => 'full_text',
		'posts.title' => 'full_text',
		// query deeply into your relations, that relations should exists on the respective models.
		'posts.comments.owner.name' => 'full_text'
	];

	public function posts()
	{
		return $this->hasMany(Post::class);
	}
}

And in your controller or anywhere:

$users = User::search('Jonh')->get();

// or replace the default searchable fields:

$users = User::search('Jonh', ['name' => 'full_text'])->get();

Note: The search method is a scope, so you need to use query builder methods like get or all to perform the search and get a collection of the results.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-09-15