定制 nh/searchable 二次开发

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

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

nh/searchable

Composer 安装命令:

composer require nh/searchable

包简介

Make a model searchable and keep Search in session

README 文档

README

composer require nh/searchable

Controller

In your controller add the Facade accessor:

use Nh\Searchable\Search;

Next, add the middleware search in the __construct() method: The search middleware will automatically redirect if there is a search session

/**
 * Instantiate a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('search:key')->only('index');
}

Finnaly, add a search() method:

/**
 * Display a listing of the searched resource.
 * @param \Illuminate\Http\Request $request
 * @return \Illuminate\Http\Response
 */
public function search(Request $request)
{
    // Make a Search Class
    $search = new Search('key', $request->input('search'));

    // Get an attribute in Search Class
    $keywords = $search->attribute('text');

    // Make the search query
    $posts = Post::search($keywords,'contains',false)->get();

    // Display the result
    return view('my.search.view', compact('posts'));
}

The Search class will create a session with:

  • the key (should be your model ex: posts)
  • the redirections (by default: 'reset' => 'myroute/index' and 'search' => 'myroute/search')
  • the attributes

The $request->input('search') must be an array of field, like search['text'].

You can override the redirections with an array:

$search = new Search('key', $request->input('search'), [
  'reset' => 'custom.redirection',
  'search' => 'custom.redirection'
]);

You can access to a Search redirection by:

session('search.key')->redirection('reset');

You can access to a Search attribute by:

session('search.key')->attribute('text');

Model

Add the Searchable trait to your model:

use Nh\Searchable\Traits\Searchable;

use Searchable;

And you can set the columns where to make the search: You can set some relationship column too as 'relation.column'

/**
 * The searchable columns.
 *
 * @var array
 */
protected $searchable = [
  'title', 'subtitle', 'description', 'author.firstname'
];

Then you can make a search query as: The search can be 'contains', 'start' or 'end' And you can decide if all columns match by setting true/false

Post::search('my keyword','contains',false)->get();

You can also search between 2 values: Default values are 0 to 99999999999999999999

Post::searchBetween('mycolumn',10,100)->get();    // Retrieve where between 10 and 100
Post::searchBetween('mycolumn',null,100)->get();  // Retrieve where under or equal 100
Post::searchBetween('mycolumn',100,null)->get();  // Retrieve where greater or equal than 100
Post::searchBetween('mycolumn',0,0)->get();       // Retrieve where is 0 or null

Routes

You can reset a Search Session by going on the route searchable.reset:

route('searchable.reset', ['key' => 'key'])

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-04-06

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固