Created
March 4, 2016 17:56
-
-
Save diegomengarda/512f4318f202c285cc81 to your computer and use it in GitHub Desktop.
paginação laravel
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 namespace Admin\Http\Controllers\Admin; | |
use Admin\Http\Requests\Admin\UserRequest; | |
use Admin\Http\Controllers\Controller; | |
use Admin\Repositories\RoleRepository; | |
use Admin\Repositories\UserRepository; | |
use Admin\User; | |
use Illuminate\Http\Request; | |
use Illuminate\Database\Eloquent\Collection as EloquentCollection; | |
use Illuminate\Pagination\AbstractPaginator as Paginator; | |
use Illuminate\Support\Facades\DB; | |
class UserController extends Controller | |
{ | |
protected $userRepository; | |
protected $roleRepository; | |
protected $request; | |
function __construct(UserRepository $userRepository, RoleRepository $roleRepository, Request $request) | |
{ | |
$this->userRepository = $userRepository; | |
$this->roleRepository = $roleRepository; | |
$this->request = $request; | |
} | |
public function index() | |
{ | |
$request = $this->request->all(); | |
if (isset($request['filter_clean'])) { | |
return redirect()->route('users'); | |
} | |
$list = $this->userRepository->getAllByParams($request, 10); | |
return view('admin.users.index', ['listagem'=> $list, 'paginate' => true]); | |
} |
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 | |
namespace Admin\Queries; | |
use Illuminate\Database\Query\Builder as QueryBuilder; | |
use Illuminate\Database\Eloquent\Builder as EloquentQueryBuilder; | |
class UserQueryFilter extends BaseQueryBuilder | |
{ | |
/** | |
* MovementsFilterQuery constructor. | |
* @param EloquentQueryBuilder|QueryBuilder $query | |
* | |
* @param array $params | |
*/ | |
public function __construct($query, array $params) | |
{ | |
$this->params = $params; | |
$this->query = $query; | |
} | |
private function byName() | |
{ | |
$name = array_get($this->params, 'name'); | |
if (is_null($name) || strlen($name) < 1) { | |
return; | |
} | |
$this->query->where('name', 'LIKE', '%'.$name.'%'); | |
} | |
private function byLogin() | |
{ | |
$login = array_get($this->params, 'login'); | |
if (is_null($login) || strlen($login) < 1) { | |
return; | |
} | |
$this->query->where('login', 'LIKE', '%'.$login.'%'); | |
} | |
/** | |
* @return EloquentQueryBuilder|QueryBuilder | |
*/ | |
public function getQuery() | |
{ | |
$this->byName(); | |
$this->byLogin(); | |
return $this->query; | |
} | |
} |
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 namespace Admin\Repositories; | |
use Admin\Queries\UserQueryFilter; | |
use Admin\User; | |
use Illuminate\Pagination\AbstractPaginator; | |
class UserRepository extends BaseRepository | |
{ | |
protected $modelClass; | |
public function __construct() | |
{ | |
$this->modelClass = User::class; | |
} | |
/** | |
* Retrieve items based on informed parameters. | |
* | |
* @param array $params | |
* @param int $take | |
* @param bool $paginate | |
* | |
* @return AbstractPaginator | |
*/ | |
public function getAllByParams(array $params, $take = 15, $paginate = true) | |
{ | |
$query = (new UserQueryFilter($this->newQuery(), $params))->getQuery(); | |
return $this->doQuery($query, $take, $paginate); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment