bllim/laravel4-datatables-package — pull request #111 by ktunkiewicz (2014-07-15 12:42:21+00:00)
Title: filter_column - versatile filtering method

Hi, this is my first pull request so please be forgiving.

filter_column is an aproach to more versatile filtering than just LIKE %...%.
This function uses QueryBuilder's builtin functions by simply passing all parameters to the choosen QueryBuilder method.

# The syntax:
`filter_column ( $column_name, $method, $param_1, $param_2 ... $param_n )`

Tips:
* `$column name` - is the same as in edit_column / remove_column
* `$method` - can be any of QueryBuilder methods (where, whereIn, whereBetween, having etc.)
* `$param_n` - these can be of type:
* `string`
* `DB::raw()`
* `function` - or any other callable
* `array` of any above
* the searched value is passed to the query by placing `$1` anywhere in parameters. If callable (function) is used the searched value is passed to callable as first parameter. The callable must returns value that will be passed to the QueryBuilder's function.

# Examples:

In my projects I often use filtering/searching on datatables, so I need more universal aproach of filtering the query results. For example if i need to search for an "Order" with "id = 123" i would like to see only **one** order as a result, **not** every result that has "123" in it's id (like 1234, 5123, 1230 etc.)
This can be made like this:

## simple search

searching for value: `123`

code:
```php
$posts= Post::select(array('id','name','content'));
return Datatables::of($orders)
->filter_column('id', 'where', 'id', '=', '$1')
->make();
```

this translates to QueryBuilder's `where ('id', '=', 123)`
so the result SQL is `WHERE id = 123`

Often the data you want to search for must be somehow prepared. You can use DB::raw() to do this.
The DB::raw() can output literaly everything into the query, for example **subqueries** or **branching** if you need some really sophisticated wheres.

## simple use of DB:raw()

searching for value: `FOo`
code:
```php
$books= Book::select(array('id','code','name'));
return Datatables::of($books)
->filter_column('code', 'where', 'code', '=', DB::raw('UPPER($1)'))
->make();
```

this translates to QueryBuilder's `where ('code', '=', DB::raw('UPPER($1)'))`
so the result SQL is `WHERE code = UPPER("FOo")`

Some QueryBuilder methods need an array as parameter, this can be done using inline function.
Also if you need do some more complicated input data filtering / parsing this can be done inside function and then returned into search query.

## using callable

searching for value: `Cat,Dog,Elephant`

code:
```php
$friends = Friend::left_join('pets','friend.pets_id','=','pets.id')->select(array('friend.id','friend.name','pets.name as petsName'));

return Datatables::of($friends)
->filter_column('petsName', 'whereIn', 'pets.name', function($value) { return explode(',',$value); })
->make();
```

this translates to QueryBuilder's `whereIn ('pets.name', array( 'Cat', 'Dog', 'Elephant' ) )`
so the result SQL is `WHERE pets.name IN = ( "Cat", "Dog", "Elephant" ) `