2017 © Pedro PelĂĄez
 

library search-box

Laravel package for admin search box.

image

andrewboy/search-box

Laravel package for admin search box.

  • Friday, June 17, 2016
  • by andrewboy
  • Repository
  • 1 Watchers
  • 2 Stars
  • 215 Installations
  • JavaScript
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 4 % Grown

The README.md

SearchBox

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)

Installation

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"

Set up javascript plugin

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();

Javascript events

$('.searchbox').searchBox({
    itemBeforeInit(){},     //before search item init
    itemAfterInit(item){}   //after search item init
});

You have two choice to pass the search params, (*10)

1. Just create the 'searchParams' php variable in the controller and pass it to the view

Explained in the controller section

2. You can pass the parameters through the plugin

$('.searchbox').searchBox({
    params: {{ searchParams }}
});

Set up Model

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;
}

Use Models' realtions with the fast and easy way

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]
]

Set up the controller

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());

Set language

default language set is hu, en, (*20)

you can extend the language in 'resources/lang/vendor/search-box', (*21)

Set view

to insert the view, (*22)

@include('search-box::searchBox')

Built in filters

Integer

Operators: equals (=), not equals (!=), greater than or equal (>=), less than or equal (=), (<),, (*23)

Date

Operators: equals (=), greater than or equal (>=), less than or equal (<=), not equal (><), (*24)

String

Operators: contains (~), not contains (!~), (*25)

Boolean

Operators: true (!!1), false (!!0), (*26)

List

Operators: equals (=), not equals (!=), (*27)

write down how to publish the parts of the code etc., (*28)

Options

The "getSearchSet" method 3rd parameter is the options parameter. The type of the parameter is array., (*29)

Caching

'is_caching'    =>  true

The Versions

17/06 2016

dev-master

9999999-dev

Laravel package for admin search box.

  Sources   Download

The Requires

 

by Beck AndrĂĄs

laravel search model admin