dev-master
9999999-dev
The Requires
- php >=5.4.0
The Development Requires
by Philippe MICHEL
dev-has_count
dev-has_count
The Requires
- php >=5.4.0
The Development Requires
by Philippe MICHEL
Wallogit.com
2017 © Pedro Peláez
Metasearch is a package for Laravel 5. It allows you to easily create an Eloquent query that match with a search form. By using a list of keywords on your search form input fields names, Metasearch_l5 will automatically create the corresponding query . It's more or less a port of activerecord-hackery/meta_search. Right n now i'm using it a lot in Rest web services, as it allows great flexibility when you need some search capabilities., (*1)
Here is a simple example :, (*2)
first the search form :, (*3)
{{ Form::open(['url' => URL::route('cars.search')]) }}
{{ Form::label('year : ') }}
{{ Form::text('year_equals') }}
{{ Form::label('minumum horsepower : ') }}
{{ Form::text('horsepower_greater_than') }}
{{ Form::label('maximum horsepower : ') }}
{{ Form::text('horsepower_less_than') }}
{{ Form::submit('submit') }}
{{ Form::close() }}
Then in your CarsController :, (*4)
$input = Request::all();
$query = Search::getQuery(Car::getQuery(), $input);
$cars = $query->get();
return view('cars.index', ['cars' => $cars]);
And that's it, the getQuery method will create the Query based on the Input content., (*5)
first add the dependency to uyour composer.json file :, (*6)
"require": {
"elfif/MetaSearch_L5" : "*"
}
and do a composer install., (*7)
Then edit your app.php file. Add the service provider for this package :, (*8)
'Elfif\MetaSearch\MetaSearchServiceProvider'
and add the facade :, (*9)
'Search' => 'Elfif\MetaSearch\Facades\Search'
Now you are good to go !!, (*10)
Here is a list of all the keywords you can use with LaraSearch, based on their datatypes, (*11)
If you'd like to match on one of several possible columns, you can do this:, (*12)
{{ Form::label('name : ') }}
{{ Form::text('name_or_brand_equals') }}
Just keep in mind the condition will be the same for all columns., (*13)
You can also check for a relation existence using the keyword "exist" like that. Let's say we have a relation called accessories in our car model :, (*14)
public function accessories(){
return $this->hasMany('App\Accessory');
}
We can request that relation's existence using the keyword "_exists" (or _ex in short) this way :, (*15)
{{ Form::label('has accessories : ') }}
{{ Form::checkbox('accessories_exists') }}
If the checkbox is checked it will add that condition to the query builder, (*16)
->has('accessories');
You may also specify an operator and count to further customize the query using the keyword "count" (or _co in short) plus a keyword to define the condition, (*17)
{{ Form::label('has accessories : ') }}
{{ Form::text('accessories_count_greater_than') }}
Will translate into :, (*18)
->has('accessories', '>=', $value);
If you need even more control over your conditions regarding a relation you can use that notation, with a dot between the relation and the field's name, followed by a condition, (*19)
{{ Form::label('accessories : ') }}
{{ Form::text('accessories.type_contains') }}
It will translate into :, (*20)
->whereHas('accessories', function($query){
return $query->where('type', 'like', '%'.$value.'%');
});