Laravel Model Searching with nicolaslopezj/searchable
Laravel
1 week ago
nicolaslopezj/searchable: A PHP Trait for Easy Laravel Model Searching
This package provides a convenient way to add search functionality to your Laravel models. By using the SearchableTrait
, you can easily define searchable fields and perform simple or complex searches with minimal effort.
1. Installation
- Install via Composer:
Bash
composer require nicolaslopezj/searchable
2. Include the Trait
- In your model:
PHP
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Nicolaslopezj\Searchable\SearchableTrait;
class User extends Model
{
use SearchableTrait;
protected $searchable = [
'columns' => [
'users.name' => 10,
'users.email' => 5,
],
];
}
3. Usage
- Basic Search:
PHP
$users = User::search('John Doe')->get();
- With Pagination:
PHP
$users = User::search('John Doe')->paginate(15);
- Full Text Search:
PHP
$users = User::search('John Doe', null, true)->get();
Explanation
$searchable
: Defines the fields to search and their weights. Higher weight means higher priority in the search results.- Important: Include the table name (e.g.,
users.name
) to ensure the correct columns are targeted.
- Important: Include the table name (e.g.,
search()
method:- Takes the search term as the first argument.
- Optionally takes a
$callback
function for custom search logic. - The third argument (
$fullTextSearch
) enables full text search (e.g., searching for the entire phrase "John Doe" instead of individual words).
Example with Custom Callback
PHP
$users = User::search('John Doe', function ($query, $term) {
// Add custom logic here, e.g., searching in a related table
return $query->whereHas('posts', function ($q) use ($term) {
$q->where('title', 'like', "%{$term}%");
});
})->get();
Benefits of Using nicolaslopezj/searchable
- Simplified Search Implementation: Easily define searchable fields and create complex search queries with minimal code.
- Improved Code Readability: Encapsulates search logic within the model, making your code cleaner and more maintainable.
- Flexible Search Customization:
- Customize search weights for different fields.
- Modify search logic to suit specific requirements.
- Enhanced Performance: Efficiently handles search operations, especially with large datasets.
- Reduced Development Time: Streamlines the process of adding search functionality to your Laravel models
Key Points
- Simplicity: Easy to implement and use.
- Flexibility: Allows for customization of search logic and weights.
- Maintainability: Encapsulates search logic within the model.
- Security: Sanitize user input before using it in search queries to prevent SQL injection vulnerabilities.
Note:
- This package is designed for basic search functionality. For complex or high-volume searches, consider using dedicated search engines like Elasticsearch or Algolia.
- MySQL
ONLY_FULL_GROUP_BY
:- If you encounter issues with the
SearchableTrait
and haveONLY_FULL_GROUP_BY
enabled in your MySQL configuration, you might need to temporarily disable it for this specific functionality. - Disabling
ONLY_FULL_GROUP_BY
can have security and data integrity implications. It's crucial to understand the potential risks before making any changes to your MySQL configuration. Refer to the provided resource ([Laravel: Unleashing the Power of MySQL Strict Mode]) for guidance on disablingONLY_FULL_GROUP_BY
in Laravel.
- If you encounter issues with the
By following these steps and considering the potential drawbacks of disabling ONLY_FULL_GROUP_BY
, you can effectively use the nicolaslopezj/searchable
trait for your Laravel search needs.