0
votes

Im making a proyect with Laravel 4.2, bootstrap and MySQL. Right now im having the following problem.

I retrieve database records and then i show them using pagination. I do this in the same way for several pages, so i created a blade template to not repeat myself. The problem comes when i use the links method of the Paginator. With a null argument the method doesn´t throw any exception but it doesn´t work properly since the links always redirects you to the main page no matter where you are.

In the Laravel 4.2 docs the following is said:

If you would like to specify a custom view to use for pagination, you may pass a view to the links method:

<?php echo $users->links('view.name'); ?>

But if i try to do something like this in my blade template:

{{ $products->links('client.pages.search_results') }}

I get the following exception:

Method Illuminate\View\View::__toString() must not throw an exception

EDIT

This is the search_results template:

@extends('client.layouts.client')
@section('content')

    @include('client.includes.products', ['header_title' => "Results for $search",
                                          'show_platform_icon' => true,
                                          'view_name' => 'client.pages.search_results'])

@stop

And this is the products template:

<div>
    <div class="navbar navbar-default">

        <div class="navbar-collapse navbar-header">
            <h3>{{ $header_title }}</h3>
        </div>
        <div class="navbar-collapse navbar-right">
            {{ Form::open(['class' => 'navbar-form']) }}
            <div class="form-group">
                <select class="form-control" id="sorter" >
                    <option disabled selected>Order by</option>
                    <option value="{{ URL::route('index') }}/name/asc">Name asc</option>
                    <option value="{{ URL::route('index') }}/name/desc">Name desc</option>
                    @if(Auth::check())
                    <option value="{{ URL::route('index') }}/discount/asc">Discount asc</option>
                    <option value="{{ URL::route('index') }}/discount/desc">Discount desc</option>
                    @else
                    <option value="{{ URL::route('index') }}/price/asc">Price asc</option>
                    <option value="{{ URL::route('index') }}/price/desc">Price desc</option>
                    @endif
                </select>
            </div>
            {{ Form::close() }}
        </div>
    </div>

    <div class="col-md-12">
        <ul class="thumnails col-md-11">
            @foreach ($products as $index => $product)
            <li class="col-md-3 col-md-offset-1 thumbnail">
                <a href="#product">
                    {{ HTML::image($product->game->thumbnail_image_path, $product->game->name) }}
                </a>

                <div class="caption">
                    @if($show_platform_icon)
                    <a href="#platform" class="pull-left">
                        {{ HTML::image($product->platform->icon_path, $product->platform->name) }}
                    </a>
                    @endif
                    <a href="#product">
                        <div class="text-center">

                            @if(Auth::check() && $product->discount)
                            <h3>
                                {{ floatval($product->price * ((100 - $product->discount) / 100)) }} € 
                                <span class="label label-default">-{{ floatval($product->discount)}}%</span>
                            </h3>
                            @else
                            <h3>{{ floatval($product->price) }} €</h3>
                            @endif
                        </div>
                    </a>
                </div>
            </li>
            @endforeach
        </ul>
        <div class="col-md-4 col-md-offset-4 text-center">
            {{ $products->links($view_name) }}
        </div>
    </div>    
</div>
1
Can you show us client.pages.search_results? I think the error is somewhere in that view.lukasgeiter
I edited my question. You can appreciate i pass the view name to the products template in order to make it work dynamically.ivan0590
I think you misunderstand how links('view') works. The view you pass will be used to display the pagination links. To give you an idea, this is the default one: slider-3.phplukasgeiter
How do the links in the rendered page look like? Should be something with ?page=1lukasgeiter
I fixed the issue. I was screwing up the pagination process with one of my routes. The paginator was trying to access the page segment but the route was redirecting that request to the main page. I couldn't do it without your help.ivan0590

1 Answers

0
votes

From the comments below the question

I think you misunderstand how links('view') works. The view you pass will be used to display the pagination links. To give you an idea, this is the default one: slider-3.php

Ok, i see my error. Now i have to figure out why when i use links() it redirects me to the main page instead of staying in the actual page. Thanks a lot.

How do the links in the rendered page look like? Should be something with ?page=1

a few hours later...

I fixed the issue. I was screwing up the pagination process with one of my routes. The paginator was trying to access the page segment but the route was redirecting that request to the main page. I couldn't do it without your help.