2
votes

I have a controller que find the products and return to view . Current I'm trying make the pagination and I'm using the following code :

return $this->queryProductsAvailable()
                ->where('fornecedores.plano_id', $plano)
                ->select('produtos.*')
                ->paginate( $limit );      

function queryProductsAvailabe:

private function queryProductsAvailable()
{   
   return Products::join('fornecedores', 'fornecedores.id', '=' ,'produtos.fornecedor_id')
                        ->where( 'fornecedores.ativo', '1')
                        ->where( 'produtos.moderacao', 'P' )
                        ->where( 'produtos.ativo' , '1' )  
                        ->where( 'produtos.preco_min', '>', 0 )
                        ->select( 'produtos.*');                   
}

To view I'm return the following:

return view('Catalogo.index', 
                    [ 
                        'products' => $products
                    ] 
                );

But when I run the method $products->render() nothing happens.
No errors, no messages...
if I run $products->total() is returned the value 0
if I run $products->nextPageUrl() nothing happens

The same variable used to access this methods is used for the loop and print of products and the foreach works fine.

if I access the url with /?page=2 the pagination works, but not render list of pages.

Any idea?

---- Edite ----

The code works as follows:

In my site there are plans (plans 5 levels) that will make the products are listed before the other, then the search for the product I do the following:

In the first search I seek 30 level products 5 if the level 5 does not have 30 products I check how many products brought to query, I reduce the 30 and seek more product in the plan 4 to complete the 30 products and so on.

In this process, when we decrease a plan I use the push methodo to add the collection and whenever there is the decrease in the plan, the paginate limit is also being changed ...

So I have two points that may be causing this problem, the first is the push method and the second in the act of reducing the plan and seek more products with a different paginate the first one was 30.

Other than that my query also search products randomly, I believe that I need to develop my own paginate to meet all my requirements ...

Thank you all

4
How many results are you expecting? render() won't do anything if you have less than the number of results you are paginating.user1669496
hi @user3158900. My paginate receive the value 30 and the variable show 30 products. in the database exists 500 products.Dayglor

4 Answers

0
votes

The paginate() method ill only add limit and offset in your query. In order to show the results, you will need to return the view instead:

$products = $this->queryProductsAvailable()
                ->where('fornecedores.plano_id', $plano)
                ->select('produtos.*')
                ->paginate( $limit );

return view('Catalogo.index', 
                    [ 
                        'products' => $products
                    ] 
                );
0
votes

Try doing this in your view to print the pagination:

{!! $products !!}}

It won't show if you have items equal to or less than the limit you have passed to the paginate($limit) function

0
votes

I would do those things:

  • Check in your view with firebug or chrome devtools if the html is rendered;
  • Check if you're using {!! $products->render() !!} in your view;
  • Check with dd/var_dump/print_r the results of your query. Replace ->paginate(...) with ->get() and see if your query us returnig the correct values.

return $this->queryProductsAvailable() ->where('fornecedores.plano_id', $plano) ->select('produtos.*') ->get( $limit )

Try those and come back with your results. I think you are doing somenthing wrong with your query.

  • Finally, check storage/logs/laravel.log for any information.
0
votes

Paginate returns a LengthAwarePaginator Object, not a Collection. This might cause the problem.