Tips and Best Practices
Performance Considerations
- Add database indexes to frequently searched columns
- Use exact matches when possible to avoid LIKE queries
- Consider using Laravel Scout for full-text search on large datasets
- Limit the number of searchable relationships to avoid N+1 queries
Handling Empty Search Values
The package automatically handles empty or null search values gracefully - they simply won't add any conditions to your query.
Combining with Other Query Methods
Since search() returns a query builder instance, you can chain any standard Eloquent methods:
$users = User::search($searchQuery, ['%name'])
->where('verified', true)
->orderBy('created_at', 'desc')
->with('country')
->paginate(15);
API Reference
search($value, array $fields)
Adds search conditions to the query.
Parameters:
$value(mixed) - The search term or value$fields(array) - Array of searchable fields with optional operators
Returns: Query builder instance for method chaining
searchDate($value, array $fields, string $operator = '=')
Adds date-based search conditions to the query.
Parameters:
$value(string) - Date value or date range$fields(array) - Array of date fields to search$operator(string) - Comparison operator (>,<,=,>=,<=,><)
Returns: Query builder instance for method chaining