在尝试排序时,“Paginator无法转换为字符串”

时间:2014-03-01 09:39:18

标签: php laravel pagination laravel-4

我正在尝试创建排序和分页列表..但它不能正常工作..

所以我在laravel中看到了这个错误

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

这是我的代码

 /**
     * 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>

1 个答案:

答案 0 :(得分:1)

尝试使用View Composer。将以下内容存储在路线文件或其他任何内容中。

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

这将自动将查询字符串添加到所有分页中的分页链接中。您不必将其附加到所有控制器中。

在您的视图文件中,您只需致电{{ $nerds->links() }}

<强>更新

问题出在这一行:'querystr' => '&order=' . (Input::get('order') == 'asc' || null ? 'desc' : 'asc').$nerds

您正在将对象$nerds附加到字符串。即$querystr