flyingapesinc / deepsearch
Advanced model and relationships search for Laravel
Requires
- laravel/framework: >=4.2
This package is not auto-updated.
Last update: 2024-09-29 05:41:53 UTC
README
DeepSearch is a search package for laravel that use recursivity to efficiently find a record of a model, by occurrences of any word found in the search and in any relationship given for the model.
Let's say you have a model Post, the model Post hasMany comments, and the model Comment belongsTo a User. Beside, your app user searched for the string "Galactic Empire,and... pizza for steve".
Yup, that makes sense. If you want to find a post given that string in any part of the relationship chain of the Post model it could, for example:
- bring a post which title is "The love for pizza".
- bring a post that has a comment that says "Wow that was out of this empire"
- bring a post that has a comment posted by a guy named "Steve". Hi Steve.
Installation
Require the package in your composer.json and update composer to download the package
composer require flyingapesinc/deepsearch
After that, add the ServiceProvider to the providers array in config/app.php
FlyingApesInc\DeepSearch\ServiceProvider::class,
if you want to, add the facade for convenience
'DeepSearch' => FlyingApesInc\DeepSearch\Facade::class,
How to Use
Using the trait
Use the trait in your model
use FlyingApesInc\DeepSearch\Traits\DeepSearchable; class Model extends Eloquent { use DeepSearchable; ... }
The deepSearch() method will bring back your search results. It takes 3 arguments:
- $search the search string to find
- $fields an array with the fields of the model you want to search in
- $relationFields an associative array with the relations of the main model and their fields you want to search in
$posts = Post::deepSearch($userInput, ['title'], [ 'comments' => 'comment', 'comments.user' => ['name', 'lastname'] ])->get();
Using the static class
Alternatively you can use the static class DeepSearch::find(). It takes 3 arguments:
- $search the search string to find
- $model the model from which you want to return the records. ex: 'App\Post'
- $searchSchema an array with the relations of the main model you want to search in, as well as what fields. The format is as follows:
$searchSchema = [ 'fields' => ['title'], // Fields where you want to search in the main model 'relationships' => [ // Relationships, if any [ 'relationship' => 'comments', // Here you put name of the relationship 'fields' => 'comment', // And here the fields where you want to search in the related table ], [ 'relationship' => 'comments.user', // Use dot notation for inner relations 'fields' => ['name', 'lastname'], ] ] ];
Chaining
The deepSearch() and find() methods return a query, so you need to bring the results by yourself using get() or paginate(n) and even chaining other methods. Following the example above we get:
$search = DeepSearch::find($userInput, App\Post::query(), $searchSchema)->where('active', 1)->paginate(10);
Authors
- lHumanizado
- Rubenazo