Domanda

i am trying to create sorting and pagination list..but it dosent work good..

so i see this error in laravel

 Object of class Illuminate\Pagination\Paginator could not be converted to string 

and this is my code

 /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index() {
        // CACHE SORTING INPUTS
        $allowed = array('ID', 'NAME', 'EMAIL'); // add allowable columns to search on
        $sort = in_array(Input::get('sort'), $allowed) ? Input::get('sort') : 'id'; // if user type in the url a column that doesnt exist app will default to id
        $order = Input::get('order') === 'asc' ? 'asc' : 'desc'; // default desc

        $nerds = Nerd::order_by($sort, $order);
        // PAGINATION
        $nerds = $nerds->paginate(5);

        $pagination = $nerds->appends(
                        array(
                            'ID' => Input::get('ID'),
                            'NAME' => Input::get('NAME'),
                            'EMAIL' => Input::get('EMAIL'),
                            'sort' => Input::get('sort'),
                            'order' => Input::get('order')
                ))->links();

        // load the view and pass the nerds     
        return View::make('nerds.index')->with(
                        array(
                            'nerds' => $nerds,
                            'pagination' => $pagination,
                            'querystr' => '&order=' . (Input::get('order') == 'asc' || null ? 'desc' : 'asc').$nerds
        ));
    }

index.blade.php

<!DOCTYPE html>
<html>
    <head>
        <title>Look! I'm CRUDding</title>
        <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
    </head>
    <body>
        <div class="container">

            <nav class="navbar navbar-inverse">
                <div class="navbar-header">
                    <a class="navbar-brand" href="{{ URL::to('nerds') }}">Nerd Alert</a>
                </div>
                <ul class="nav navbar-nav">
                    <li><a href="{{ URL::to('nerds') }}">View All Nerds</a></li>
                    <li><a href="{{ URL::to('nerds/create') }}">Create a Nerd</a>
                </ul>
            </nav>

            <h1>All the Nerds</h1>

            <!-- will be used to show any messages -->
            @if (Session::has('message'))
            <div class="alert alert-info">{{ Session::get('message') }}</div>
            @endif

            <table class="table table-striped table-bordered">
                <thead>
                    <tr>
                        <th><a href="<?= URL::to('nerds?sort=id' . $querystr) ?>">ID</a></th>
                        <th><a href="<?= URL::to('nerds?sort=NAME' . $querystr) ?>">Name</a></th>
                        <th><a href="<?= URL::to('nerds?sort=EMAIL' . $querystr) ?>">Email</a></th>
                        <td>Nerd Level</td>
                        <td>Actions</td>
                    </tr>
                </thead>
                <tbody>
                    @foreach($nerds as $key => $value)
                    <tr>
                        <td>{{ $value->id }}</td>
                        <td>{{ $value->name }}</td>
                        <td>{{ $value->email }}</td>
                        <td>{{ $value->nerd_level }}</td>

                        <!-- we will also add show, edit, and delete buttons -->
                        <td>

                            <!-- delete the nerd (uses the destroy method DESTROY /nerds/{id} -->
                            <!-- we will add this later since its a little more complicated than the first two buttons -->
                            {{ Form::open(array('url' => 'nerds/' . $value->id, 'class' => 'pull-right')) }}
                            {{ Form::hidden('_method', 'DELETE') }}
                            {{ Form::submit('Delete this Nerd', array('class' => 'btn btn-warning')) }}
                            {{ Form::close() }}

                            <!-- show the nerd (uses the show method found at GET /nerds/{id} -->
                            <a class="btn btn-small btn-success" href="{{ URL::to('nerds/' . $value->id) }}">Show this Nerd</a>

                            <!-- edit this nerd (uses the edit method found at GET /nerds/{id}/edit -->
                            <a class="btn btn-small btn-info" href="{{ URL::to('nerds/' . $value->id . '/edit') }}">Edit this Nerd</a>

                        </td>
                    </tr>
                    @endforeach
                </tbody>
            </table>

         {{ $nerds->links() }}
        </div>
    </body>
</html>
È stato utile?

Soluzione

Try with View Composer. Store the below in your routes file or whatever.

View::composer(Paginator::getViewName(), function($view) {
    $queryString = array_except(Input::query(), Paginator::getPageName());
    $view->paginator->appends($queryString);
});

This will automatically add query strings to your pagination links in all pagination. You dont have to append it in all your controllers.

In your view file, you just have to call {{ $nerds->links() }}

UPDATE

Problem is with this line: 'querystr' => '&order=' . (Input::get('order') == 'asc' || null ? 'desc' : 'asc').$nerds

You are appending an object $nerds to a string. i.e $querystr

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top