![]()
WARNING You're browsing the documentation for an upcoming version of Laravel DataTables. The documentation and features of this release are subject to change.
Smart Search
Smart search controls how DataTables matches search keywords against your data. The package offers multiple search modes to suit different use cases.
Search Modes Overview
| Mode | Behavior | Use Case |
|---|---|---|
| Default (Smart) | %keyword% - wraps with wildcards |
General search |
| Starts With | keyword% - prefix only |
Autocomplete, suggestions |
| Ends With | %keyword - suffix only |
File extensions, codes |
| Exact Match | keyword - no wildcards |
IDs, codes, exact terms |
Basic Usage
By default, smart search is enabled with automatic wildcards:
Route::get('user-data', function () { return DataTables::eloquent(User::query())->toJson();});
Searching for "john" becomes %john%.
Disable Smart Search
Use ->smart(false) for exact matching:
Route::get('user-data', function () { return DataTables::eloquent(User::query()) ->smart(false) ->toJson();});
Searching for "john" matches exactly "john", not "Johnny".
Starts With Search
Search for records that start with the given keyword:
Route::get('user-data', function () { return DataTables::eloquent(User::query()) ->startsWithSearch() ->toJson();});
Behavior Comparison
When searching "John":
| Search Type | SQL Pattern | Matches |
|---|---|---|
| Default (Contains) | %John% |
"John", "Johnny", "Long John" |
| Starts With | John% |
"John", "Johnny" only |
| Ends With | %John |
"Long John" only |
| Exact Match | John |
"John" only |
How Smart Search Works
Smart search automatically transforms user input:
- Wildcard Wrapping - Terms are wrapped with
%for partial matching - Multi-word AND Logic - Spaces split terms, all must match
| User Types | Generated SQL |
|---|---|
john |
WHERE name LIKE '%john%' |
john doe |
WHERE name LIKE '%john%' AND name LIKE '%doe%' |
Global Configuration
Configure defaults in config/datatables.php:
return [ // Wrap search terms with % wildcards 'smart' => true, // Case insensitive searching 'case_insensitive' => true, // Use wildcards in search 'use_wildcards' => false,];
When to Use
- Use Smart Search (default): When you want users to find results by typing any part of a word
- Use Starts With: When building autocomplete fields or suggestions
- Disable Smart Search: When searching for exact IDs, codes, or precise values
See Also
- Filter Column - Custom column filtering
- Manual Search - Custom filtering logic
- Regex Search - Regular expression search