Laravel:对流畅的查询进行分页

时间:2013-07-31 14:11:56

标签: php laravel laravel-4 fluent blade

我正试图在我的视图中对页面进行分页,如下所示:

@foreach($tasks as $task)
    {{ $task->user_id }}
    {{ $task->client_id }}
    {{ $task->description }}
    {{ $task->duration }}
    {{ link_to_route('clients.show', 'View client', array($task->client_id), array('class' => 'btn btn-primary')) }}
@endforeach
    {{ $tasks->links() }}

在我的控制器中使用以下查询:

$tasks = DB::table('tasks')
    ->join('users', 'tasks.user_id', '=', 'users.id')
    ->join('clients', 'tasks.client_id', '=', 'clients.id')
    ->select(array('tasks.description', 'tasks.duration', 'tasks.client_id', 'tasks.user_id', 'users.email', 'clients.name'))
    ->where('tasks.group_id', '=', $usergroup)
    ->orderBy('tasks.created_at', 'DESC')
    ->paginate(20);

    return View::make('tasks.index', compact('tasks'));

它显示任务正常,但没有显示分页链接,因此我无法前往下一批20个结果。

关于如何使这项工作的任何想法?

我在http://forums.laravel.io/viewtopic.php?id=4092中的建议中尝试了@foreach($ tasks->结果为$ task),但它给了我一个错误“Undefined property:Illuminate \ Pagination \ Paginator :: $ result “

1 个答案:

答案 0 :(得分:4)

对于那些在家里玩的人 - 我发现了答案:

紧凑型函数将对象转换为数组。将您的返回视图方法更改为:

return View::make('tasks.index')->with('tasks', $tasks);

你明白了!

另一点 - 如果你像我一样使用Bootstrap 3 RC1,你会发现分页因为BS3样式分页的方式而中断 - 如果你有这个问题,可以先找到解决方案:https://github.com/laravel/laravel/issues/2215

:)

相关问题