dev-master
9999999-devAdvanced model and relationships search for Laravel
MIT
The Requires
by Rubén Gonzålez
by Henry Alvarez
v0.2
0.2.0.0Advanced model and relationships search for Laravel
MIT
The Requires
by Rubén Gonzålez
by Henry Alvarez
Wallogit.com
2017 © Pedro PelĂĄez
Advanced model and relationships search for Laravel
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., (*1)
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"., (*2)
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:, (*3)
Require the package in your composer.json and update composer to download the package, (*4)
composer require flyingapesinc/deepsearch
After that, add the ServiceProvider to the providers array in config/app.php, (*5)
FlyingApesInc\DeepSearch\ServiceProvider::class,
if you want to, add the facade for convenience, (*6)
'DeepSearch' => FlyingApesInc\DeepSearch\Facade::class,
Use the trait in your model, (*7)
use FlyingApesInc\DeepSearch\Traits\DeepSearchable;
class Model extends Eloquent {
use DeepSearchable;
...
}
The deepSearch() method will bring back your search results. It takes 3 arguments:, (*8)
$posts = Post::deepSearch($userInput, ['title'], [
'comments' => 'comment',
'comments.user' => ['name', 'lastname']
])->get();
Alternatively you can use the static class DeepSearch::find(). It takes 3 arguments:, (*9)
$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'],
]
]
];
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:, (*10)
$search = DeepSearch::find($userInput, App\Post::query(), $searchSchema)->where('active', 1)->paginate(10);
Advanced model and relationships search for Laravel
MIT
Advanced model and relationships search for Laravel
MIT