Top Related Projects
Driver for Laravel Scout search package based on https://github.com/teamtnt/tntsearch
Laravel Scout provides a driver based solution to searching your Eloquent models.
A php trait to search laravel models
Quick Overview
Laravel Searchable is a package that provides a simple and flexible way to add search functionality to Laravel applications. It allows developers to search through multiple models and their relationships with ease, offering a unified search interface across different database tables.
Pros
- Easy to set up and integrate with existing Laravel projects
- Supports searching through multiple models and their relationships
- Customizable search logic and weighting of results
- Minimal configuration required for basic usage
Cons
- Limited to Laravel applications only
- May not be suitable for complex search requirements or large-scale applications
- Lacks advanced features like fuzzy matching or full-text search out of the box
- Performance may degrade with large datasets or complex queries
Code Examples
- Defining a searchable model:
use Spatie\Searchable\Searchable;
use Spatie\Searchable\SearchResult;
class Post extends Model implements Searchable
{
public function getSearchResult(): SearchResult
{
return new SearchResult(
$this,
$this->title,
$this->url
);
}
}
- Performing a search:
use Spatie\Searchable\Search;
$searchResults = (new Search())
->registerModel(Post::class, 'title')
->registerModel(User::class, 'name')
->search('john');
- Customizing search logic:
use Spatie\Searchable\Search;
$searchResults = (new Search())
->registerModel(Post::class, function($query, $term) {
$query->where('title', 'LIKE', "%{$term}%")
->orWhere('content', 'LIKE', "%{$term}%");
})
->search('laravel');
Getting Started
-
Install the package via Composer:
composer require spatie/laravel-searchable
-
Implement the
Searchable
interface on your models:use Spatie\Searchable\Searchable; use Spatie\Searchable\SearchResult; class YourModel extends Model implements Searchable { public function getSearchResult(): SearchResult { // Return a search result instance } }
-
Use the
Search
class to perform searches:use Spatie\Searchable\Search; $searchResults = (new Search()) ->registerModel(YourModel::class, 'searchable_field') ->search('query');
Competitor Comparisons
Driver for Laravel Scout search package based on https://github.com/teamtnt/tntsearch
Pros of laravel-scout-tntsearch-driver
- Utilizes Laravel Scout, providing a standardized search interface
- Offers full-text search capabilities with TNTSearch
- Supports advanced features like fuzzy matching and boosting
Cons of laravel-scout-tntsearch-driver
- Requires more setup and configuration compared to laravel-searchable
- May have a steeper learning curve for developers new to Scout
Code Comparison
laravel-scout-tntsearch-driver:
use Laravel\Scout\Searchable;
class Post extends Model
{
use Searchable;
}
laravel-searchable:
use Spatie\Searchable\SearchResult;
class Post extends Model implements Searchable
{
public function getSearchResult(): SearchResult
{
// ...
}
}
The laravel-scout-tntsearch-driver uses Laravel Scout's Searchable
trait, while laravel-searchable requires implementing a custom interface. The Scout-based approach provides a more standardized method for implementing search functionality across different models.
laravel-scout-tntsearch-driver offers more advanced search capabilities and integrates well with Laravel's ecosystem through Scout. However, laravel-searchable provides a simpler, more lightweight solution that may be easier to implement for basic search needs.
Choose laravel-scout-tntsearch-driver for complex search requirements and scalability, or opt for laravel-searchable for quick and straightforward search functionality in smaller projects.
Laravel Scout provides a driver based solution to searching your Eloquent models.
Pros of Scout
- Supports multiple search engines (Algolia, MeiliSearch, Elasticsearch)
- Offers advanced features like pagination and custom index names
- Provides a queue-based indexing system for better performance
Cons of Scout
- Requires additional setup and configuration for external search engines
- Can be overkill for simple search requirements
- May have a steeper learning curve for beginners
Code Comparison
Scout:
use Laravel\Scout\Searchable;
class Post extends Model
{
use Searchable;
}
Searchable:
use Spatie\Searchable\SearchResult;
class Post extends Model implements Searchable
{
public function getSearchResult(): SearchResult
{
// ...
}
}
Key Differences
- Scout is designed for integration with external search engines, while Searchable provides a simpler, database-driven search solution
- Searchable offers a more straightforward implementation for basic search functionality
- Scout provides more advanced features and scalability for larger applications
Use Cases
- Choose Scout for complex search requirements or when using external search engines
- Opt for Searchable when you need a quick, lightweight search solution without external dependencies
Performance Considerations
- Scout can offer better performance for large datasets when using optimized search engines
- Searchable may be more efficient for smaller applications with simple search needs
A php trait to search laravel models
Pros of searchable
- More flexible search configuration with custom search rules and weights
- Supports searching across multiple models and relationships
- Offers more advanced features like geospatial search and fuzzy matching
Cons of searchable
- Less actively maintained (last update in 2021)
- More complex setup and configuration compared to laravel-searchable
- May require more performance tuning for large datasets
Code Comparison
searchable:
class User extends Model
{
use Searchable;
protected $searchable = [
'columns' => [
'users.name' => 10,
'users.email' => 5,
],
'joins' => [
'posts' => ['users.id', 'posts.user_id'],
],
];
}
laravel-searchable:
class User extends Model
{
use Searchable;
public function getSearchResult(): SearchResult
{
return new SearchResult(
$this,
$this->name,
$this->email
);
}
}
Both packages offer search functionality for Laravel applications, but searchable provides more advanced features and flexibility at the cost of complexity. laravel-searchable is simpler to set up and use, making it a good choice for basic search requirements in smaller projects.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Laravel Searchable
This package makes it easy to get structured search from a variety of sources. Here's an example where we search through some models. We already did some small preparation on the models themselves.
$searchResults = (new Search())
->registerModel(User::class, 'name')
->registerModel(BlogPost::class, 'title')
->search('john');
The search will be performed case insensitive. $searchResults
now contains all User
models that contain john
in the name
attribute and BlogPost
s that contain 'john' in the title
attribute.
In your view you can now loop over the search results:
<h1>Search</h1>
There are {{ $searchResults->count() }} results.
@foreach($searchResults->groupByType() as $type => $modelSearchResults)
<h2>{{ $type }}</h2>
@foreach($modelSearchResults as $searchResult)
<ul>
<li><a href="{{ $searchResult->url }}">{{ $searchResult->title }}</a></li>
</ul>
@endforeach
@endforeach
In this example we used models, but you can easily add a search aspect for an external API, list of files or an array of values.
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Installation
You can install the package via composer:
composer require spatie/laravel-searchable
Usage
Preparing your models
In order to search through models you'll have to let them implement the Searchable
interface.
namespace Spatie\Searchable;
interface Searchable
{
public function getSearchResult(): SearchResult;
}
You'll only need to add a getSearchResult
method to each searchable model that must return an instance of SearchResult
. Here's how it could look like for a blog post model.
use Spatie\Searchable\Searchable;
use Spatie\Searchable\SearchResult;
class BlogPost extends Model implements Searchable
{
public function getSearchResult(): SearchResult
{
$url = route('blogPost.show', $this->slug);
return new \Spatie\Searchable\SearchResult(
$this,
$this->title,
$url
);
}
}
Searching models
With the models prepared you can search them like this:
$searchResults = (new Search())
->registerModel(User::class, 'name')
->search('john');
The search will be performed case insensitive. $searchResults
now contains all User
models that contain john
in the name
attribute.
You can also pass multiple attributes to search through:
// use multiple model attributes
$searchResults = (new Search())
->registerModel(User::class, 'first_name', 'last_name')
->search('john');
// or use an array of model attributes
$searchResults = (new Search())
->registerModel(User::class, ['first_name', 'last_name'])
->search('john');
To get fine grained control you can also use a callable. This way you can also search for exact matches, apply scopes, eager load relationships, or even filter your query like you would using the query builder.
$search = (new Search())
->registerModel(User::class, function(ModelSearchAspect $modelSearchAspect) {
$modelSearchAspect
->addSearchableAttribute('name') // return results for partial matches on usernames
->addExactSearchableAttribute('email') // only return results that exactly match the e-mail address
->active()
->has('posts')
->with('roles');
});
Creating custom search aspects
You are not limited to only registering basic models as search aspects. You can easily create your own, custom search aspects by extending the SearchAspect
class.
Consider the following custom search aspect to search an external API:
class OrderSearchAspect extends SearchAspect
{
public function getResults(string $term): Collection
{
return OrderApi::searchOrders($term);
}
}
This is how you can use it:
$searchResults = (new Search())
->registerAspect(OrderSearchAspect::class)
->search('john');
Limiting aspect results
It is possible to limit the amount of results returned by each aspect by calling limitAspectResults
prior to performing the search.
$searchResults = (new Search())
->registerAspect(BlogPostAspect::class)
->limitAspectResults(50)
->search('How To');
Rendering search results
Here's an example on rendering search results:
<h1>Search</h1>
There are {{ $searchResults->count() }} results.
@foreach($searchResults->groupByType() as $type => $modelSearchResults)
<h2>{{ $type }}</h2>
@foreach($modelSearchResults as $searchResult)
<ul>
<a href="{{ $searchResult->url }}">{{ $searchResult->title }}</a>
</ul>
@endforeach
@endforeach
You can customize the $type
by adding a public property $searchableType
on your model or custom search aspect
class BlogPost extends Model implements Searchable
{
public $searchableType = 'custom named aspect';
}
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
Top Related Projects
Driver for Laravel Scout search package based on https://github.com/teamtnt/tntsearch
Laravel Scout provides a driver based solution to searching your Eloquent models.
A php trait to search laravel models
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot