Pagination with Eager Loading in Laravel

1

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);
}
    
asked by anonymous 06.02.2014 / 03:39

1 answer

1

In my case, I have a relationship between Categories and Subcategories

Categories hasMany Subcategories

Subcategories belongsTo Categories

To get results alphabetically (Category name followed by subcategory name) as it is in my code, it will not work:

$obj = $this->subcategoria->with(array('categoria' => function ($query) 
{
    $query->orderBy('categoria.cat_nome', 'ASC');
}))
->orderBy('subcategoria.sbc_nome', 'ASC');

Because the BelongsTo Category Subcategory (this returns only one record)

But if done backwards, it will work

$obj = $this->categoria->with(array('subcategoria' => function ($query) 
{
    $query->orderBy('subcategoria.sbc_nome', 'ASC');
}))
->orderBy('categoria.cat_nome', 'ASC');

But in the end I ended up opting to use JOIN .

In my case ... in the structure of my application, doing as written above would greatly increase the amount of code I would have to add, so the final structure I ended up using is as follows:

>
public function byPage($page=1, $limit=10)
{
    $result = new \StdClass;
    $result->page = $page;
    $result->limit = $limit;
    $result->totalItems = 0;
    $result->items = array();

    $query = $this->subcategoria
            ->select('cat_nome', 'sbc_id', 'sbc_nome')
            ->join('categoria', 'cat_id', '=', 'sbc_cat_id')
            ->orderBy('cat_nome', 'ASC')
            ->orderBy('sbc_nome', 'ASC');

    $articles = $query->skip( $limit * ($page-1) )
                    ->take($limit)
                    ->get();


    $result->totalItems = $this->totalSubcategorias();
    $result->items = $articles->all();

    return $result;
}
    
07.02.2014 / 20:00