I'm having a small sorting problem involving paging and Eager Loading.
What do I need to do:
-
In a part of my site, I need to bring paged results from a subcategory
-
The subcategory is a child of a category, so I have to display in a table the parent that is the category, subcategory and other data.
-
I'm using Eager Loading to bring the category
The problem is that I need to sort by category name, and then by subcategory name, and the data is not being sorted correctly.
Follow the code:
// EloquentSubcategoria
public function byPage($page=1, $limit=10)
{
$result = new \StdClass;
$result->page = $page;
$result->limit = $limit;
$result->totalItems = 0;
$result->items = array();
$obj = $this->subcategoria->with(array('categoria' => function ($query)
{
$query->orderBy('categoria.cat_nome', 'ASC');
}))
->orderBy('subcategoria.sbc_nome', 'ASC');
$articles = $obj->skip( $limit * ($page-1) )
->take($limit)
->get();
$result->totalItems = $this->totalSubcategorias();
$result->items = $articles->all();
return $result;
}
protected function totalSubcategorias()
{
return $this->subcategoria->count();
}
// SubcategoriaController
public function index()
{
// Valores inteiros definidos no app
$page = Input::get('page', 1);
$perPage = Config::get('app.perPage');
// retorna dados para paginacao
$pagiData = $this->subcategoria->byPage($page, $perPage);
// paginar
$subcategorias = Paginator::make($pagiData->items, $pagiData->totalItems, $perPage);
return View::make('subcategorias.index')->with('subcategorias', $subcategorias);
}