Last active
August 29, 2015 14:06
-
-
Save emsifa/650931e763e85ba1bf61 to your computer and use it in GitHub Desktop.
backbone pageable trait khusus eloquent model untuk mempermudah ngambil collection untuk backbone paginator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* BACKBONE PAGEABLE TRAIT | |
* Trait untuk dipake di Eloquent Model | |
* untuk sedikit mempermudah paging collection | |
* si Backbone Paginator (https://github.com/backbone-paginator/backbone.paginator) | |
*/ | |
trait BackbonePageableTrait { | |
public static function getPageableCollection($query_or_cols = array('*'), \Closure $queryModifier = null) | |
{ | |
$page = intval(array_get($_GET, 'page', 1)); | |
$limit = intval(array_get($_GET, 'per_page', 25)); | |
$sortkey = array_get($_GET, 'sort_key', 'id'); | |
$order = intval(array_get($_GET, 'order', 1)); | |
$offset = $limit * ($page - 1); | |
if(is_array($query_or_cols)) { | |
$query = static::select($query_or_cols); | |
} else { | |
$query = $query_or_cols; | |
} | |
if($queryModifier) { | |
$query = call_user_func($queryModifier, $query); | |
} | |
return $query->orderBy($sortkey, $order? 'ASC':'DESC') | |
->skip($offset) | |
->take($limit) | |
->get(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment