Minimum Search Length
The minSearchLength() API enforces a minimum number of characters before search is executed, preventing excessive queries from short search terms.
Basic Usage
use Yajra\DataTables\Facades\DataTables;use App\Models\User; Route::get('user-data', function() { return DataTables::eloquent(User::query()) ->minSearchLength(3) ->toJson();});
How It Works
When a user enters fewer characters than the minimum:
recordsTotalandrecordsFilteredare set to0- An exception is thrown with a localized error message
- The response is handled by the Error Handler configuration
Custom Error Message
The error message uses Laravel's localization system. Publish the language files:
php artisan vendor:publish --tag=datatables-lang
Then customize the message in resources/lang/{locale}/datatables.php:
return [ 'min_search_length' => 'Please enter at least :length characters to search.',];
Example
use Yajra\DataTables\Facades\DataTables;use App\Models\Post; Route::get('post-data', function() { return DataTables::eloquent(Post::query()) ->minSearchLength(4) // Require at least 4 characters ->toJson();});
If a user searches for "php" (3 characters), the DataTable will show an empty result with the configured error message.
See Also
- Manual Search - Custom search logic
- Smart Search - Wildcard search configuration
- Error Handler - Error handling configuration