dev-master
9999999-devLaravel package for admin search box.
The Requires
- php >=5.5.9
- illuminate/support >5.0
by Beck AndrĂĄs
laravel search model admin
Laravel package for admin search box.
This is a Laravel 5 package, that creates a searcbox for admin pages that uses Twitter Bootstrap., (*1)
It's under development, not recommended for production use!, (*2)
add bundle to composer:, (*3)
"andrewboy/search-box": "dev-master"
run composer:, (*4)
composer install / update
add service provider to the providers list:, (*5)
'Andrewboy\SearchBox\SearchBoxServiceProvider'
publish view, langs and public parts:, (*6)
php artisan vendor:publish --provider="Andrewboy\SearchBox\SearchBoxServiceProvider"
add javascript plugin to the site, (*7)
<script src="{{asset('vendor/search-box/js/jquery.searchBox.js')}}"></script>
or the minified version, (*8)
<script src="{{asset('vendor/search-box/js/jquery.searchBox.min.js')}}"></script>
add plugin to your javascript file, (*9)
$('.searchbox').searchBox();
$('.searchbox').searchBox({ itemBeforeInit(){}, //before search item init itemAfterInit(item){} //after search item init });
You have two choice to pass the search params, (*10)
Explained in the controller section
$('.searchbox').searchBox({ params: {{ searchParams }} });
add the trait to your models that you want to search, (*11)
use \Andrewboy\SearchBox\Traits\SearchTrait; class Banner extends Eloquent { use SearchTrait;
in the model set the attributes like:, (*12)
protected static $searchParams = [ 'id' => [ 'type' => 'integer' ], 'name' => [ 'type' => 'string' ], 'url' => [ 'type' => 'string' ], 'is_active' => [ 'type' => 'boolean' ], 'has_attachment' => [ 'type' => 'boolean' ], 'group_id' => [ 'type' => 'list', 'relation' => ['groups', 'name'] ], 'group_id' => [ 'type' => 'list', 'values' => [ 1 => 'name1', 2 => 'name2' ] ], 'group_id' => [ 'type' => 'string', 'relation' => ['groups', 'name'] //search string through relation ], 'created_at' => [ 'type' => 'date' ], ];
in the model, extend the search for special cases, (*13)
protected function extendSearch($query, array $params) { #BANNER_PLACE_ID if (isset($params['banner_place_id']) && self::isValidSearchParam($params['banner_place_id'])) { switch ($params['banner_place_id']['operator']) { case '=': $query->whereIn('banner_place_id', $params['banner_place_id']['values']); break; case '!=': $query->whereNotIn('banner_place_id', $params['banner_place_id']['values']); break; } unset($params['banner_place_id']); } #HAS_ATTACHMENT if (isset($params['has_attachment']) && self::isValidSearchParam($params['has_attachment'])) { switch ($params['has_attachment']['operator']) { case '=': $query->has('attachment', 'LIKE', intval($params['has_attachment']['values'][0])); break; case '!=': $query->has('attachment', 'NOT LIKE', intval($params['has_attachment']['values'][0])); break; } unset($params['has_attachment']); } return $params; }
when you want to reach the models' relations, the you have to define like this, (*14)
protected static $searchParams = [ 'group_id' => [ 'type' => 'list', 'relation' => ['groups', 'name'] ], ];
where the type is set to 'list', (*15)
the relation is set by, (*16)
[ [relation_method_name], [attribute_name_which_you_want_to_use_in_dropdown] ]
in the controller, pass the search params:, (*17)
$extended = []; //you don't have to use it, if it's empty return View::make('banners.index', array('banners' => $banners)) ->with( 'searchParams', Banner::getSearchSet( route("banners.index"), $extended ) )
in the controller you can extend the previosly filled search settings:, (*18)
$extended = [ 'banner_place_id' => [ 'type' => 'list', 'values' => Banner::$bannerPlaces ] ];
in the controller, you can use it to search, (*19)
$banners = Banner::search(Input::all());
default language set is hu, en, (*20)
you can extend the language in 'resources/lang/vendor/search-box', (*21)
to insert the view, (*22)
@include('search-box::searchBox')
Operators: equals (=), not equals (!=), greater than or equal (>=), less than or equal (=), (<),, (*23)
Operators: equals (=), greater than or equal (>=), less than or equal (<=), not equal (><), (*24)
Operators: contains (~), not contains (!~), (*25)
Operators: true (!!1), false (!!0), (*26)
Operators: equals (=), not equals (!=), (*27)
write down how to publish the parts of the code etc., (*28)
The "getSearchSet" method 3rd parameter is the options parameter. The type of the parameter is array., (*29)
'is_caching' => true
Laravel package for admin search box.
laravel search model admin