Shortcut table search

Quick Search is another way to search tabular data in addition to filter, to quickly filter the data you want, open the following way:

  1. $grid->quickSearch();
  2. // Set form prompt value
  3. $grid->quickSearch()->placeholder('search...');

This will bring up a search box in the table header: Shortcut table search - 图1

By passing different parameters to the quickSearch method, to set up different search methods, there are several ways to use the following

Like search

The first way to do a simple like query is by setting the field name

  1. $grid->quickSearch('title');
  2. // After submission, the model performs the following queries
  3. $model->where('title', 'like', "%{$input}%");

Or do like queries on multiple fields:

  1. $grid->quickSearch('title', 'desc', 'content');
  2. // or
  3. $grid->quickSearch(['title', 'desc', 'content']);
  4. // After submission, the model performs the following queries
  5. $model->where('title', 'like', "%{$input}%")
  6. ->orWhere('desc', 'like', "%{$input}%")
  7. ->orWhere('content', 'like', "%{$input}%");

relationships

If dcat/laravel-wherehasin is installed, the whereHasIn method will be used in preference to queries

  1. $grid->quickSearch('user.name', 'user.username', 'content');

Custom search

The second way gives you more flexible control over your search criteria

  1. $grid->quickSearch(function ($model, $query) {
  2. $model->where('title', $query)->orWhere('desc', 'like', "%{$query}%");
  3. });

where the parameter $query of the closure is the contents of the search box you fill in, and then submit it for the search in the closure.

Quick Syntax Search

The third way references the search syntax of Github to perform a quick search, invoked as follows:

  1. // non-parametric
  2. $grid->quickSearch();

The following syntax will be used to fill in the search box, and the corresponding query will be made after submission :

Compare queries

title:footitle:!foo

  1. $model->where('title', 'foo');
  2. $model->where('title', '!=', 'foo');

rate:>10rate:<10rate:>=10rate:<=10

  1. $model->where('rate', '>', 10);
  2. $model->where('rate', '<', 10);
  3. $model->where('rate', '>=', 10);
  4. $model->where('rate', '<=', 10);

In, NotIn queries

status:(1,2,3,4)status:!(1,2,3,4)

  1. $model->whereIn('status', [1,2,3,4]);
  2. $model->whereNotIn('status', [1,2,3,4]);

Between queries

score:[1,10]

  1. $model->whereBetween('score', [1, 10]);

Time and date function queries

created_at:date,2019-06-08

  1. $model->whereDate('created_at', '2019-06-08');

created_at:time,09:57:45

  1. $model->whereTime('created_at', '09:57:45');

created_at:day,08

  1. $model->whereDay('created_at', '08');

created_at:month,06

  1. $model->whereMonth('created_at', '06');

created_at:year,2019

  1. $model->whereYear('created_at', '2019');

Like Search

content:%Laudantium%content:Laud%

  1. $model->where('content', 'like', '%Laudantium%');
  2. $model->where('content', 'like', 'Laud%');

regular query

username:/song/

{tip} Please use MYSQL regular syntax here

  1. $model->where('username', 'REGEXP', 'song');

Multi-criteria combination search

Separate multiple search statements with spaces to achieve multiple fields of the AND query, such as username:%song% status:(1,2,3), after submission will run the following search

  1. $model->where('username', 'like', '%song%')->whereIn('status', [1, 2, 3]);

If a condition is an OR query, just add a | symbol in front of the statement cell: username:%song% |status:(1,2,3)

  1. $model->where('username', 'like', '%song%')->orWhereIn('status', [1, 2, 3]);

{tip} If the filled query text contains spaces, it needs to be inside double quotes: updated_at: "2019-06-08 09:57:45"

Label as a query field name

If it is not convenient to get the field name, you can simply use the label name as the query field.

  1. // For example, if the table header of `user_status` is set to `user_status`.
  2. $grid->column('user_status', 'user_status');

Then you can fill in user status:(1,2,3) to execute the following query

  1. $model->whereIn('user_status', [1, 2, 3]);

Disable automatic submission

If you don’t want auto-submission, you can disable it in the following ways

{tip} When the auto-submit feature is disabled, you need to search by pressing the Enter key.

  1. $grid->quickSearch()->auto(false);