Last active
August 29, 2015 14:09
-
-
Save jaggy/e17a6390d38426186bdf to your computer and use it in GitHub Desktop.
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 | |
// model | |
public function paginate($page = 1, $order = [], $limit = 10) | |
{ | |
// default values | |
$order = ($order) ?: []; | |
$limit = ($limit) ?: 10; | |
$query = $this->product->take($limit); | |
if ($page > 1) { | |
$offset = ($page - 1) * $limit; | |
$query->skip($offset); | |
} | |
foreach ($order as $field => $type) { | |
if (is_integer($field)) { | |
$field = $type; | |
$type = 'asc'; | |
} | |
$query->orderBy($field, $type); | |
} | |
return $query->get(); | |
} | |
// controller | |
public function index() | |
{ | |
$page = Request::get('page'); | |
$limit = Request::get('limit'); | |
$order = Request::get('order'); | |
$sort = Request::get('sort'); | |
if ($order) { | |
$order = [$order => $sort]; | |
} | |
return $this->repository->paginate($page, $order, $limit); | |
} |
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 | |
// model | |
// should I not use boolean and just use asc or desc? | |
public function paginate($page = 1, $limit = 10, $order = 'id', $descending = false) | |
{ | |
// default values for when used in a controller | |
// I'm still a bit ticked off here because I have to reinitialize them when taking values from the controller | |
$order = ($order) ?: 'id'; | |
$limit = ($limit) ?: 10; | |
$sort = ($descending) ? 'desc' : 'asc'; | |
$offset = ($page - 1) * $limit; | |
$products = $this->product->take($limit)->orderBy($order, $sort); | |
if ($page > 1) { | |
$products->skip($offset); | |
} | |
return $products->get(); | |
} | |
// controller | |
// There's still a little bit of login in the controller | |
// A little bit nitpicky but it's annoying | |
public function index() | |
{ | |
$page = Request::get('page'); | |
$limit = Request::get('limit'); | |
$order = Request::get('order'); | |
$sort = (Request::get('sort') == 'desc') ? true : false; // how? | |
return $this->repository->paginate($page, $limit, $order, $sort); | |
} |
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 | |
// model | |
// I'm not happy but it's good enough to pass for now | |
public function paginate($page = 1, $limit = 10, $order = 'id', $sort = 'asc') | |
{ | |
// default values for when used in a controller | |
$order = ($order) ?: 'id'; | |
$limit = ($limit) ?: 10; | |
$offset = ($page - 1) * $limit; | |
$products = $this->product->take($limit)->orderBy($order, $sort); | |
if ($page > 1) { | |
$products->skip($offset); | |
} | |
return $products->get(); | |
} | |
// controller | |
// no more logic | |
public function index() | |
{ | |
$page = Request::get('page'); | |
$limit = Request::get('limit'); | |
$order = Request::get('order'); | |
$sort = Request::get('sort'); | |
return $this->repository->paginate($page, $limit, $order, $sort); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment