Laravel分页错误

时间:2019-06-26 07:56:19

标签: php laravel pagination

我需要从数据库中获取特定字段,然后对结果进行分页,但是我不确定将->paginate()放在何处。

我的代码如下:

Upload::where('sponsor_id', Request::segment(2))
        //->paginate(20)
        ->join('users', 'users.id', 'uploads.uploaded_by')
        ->orderBy('uploads.created_at', 'DESC')
        ->get([
            'uploads.filename',
            'users.first_name',
            'users.surname',
            'uploads.errors',
            'uploads.statistics',
            'uploads.created_at',
        ])

我没有分页就得到正确的结果,但我想对结果进行分页。

2 个答案:

答案 0 :(得分:7)

首先,您需要使用select然后传递分页。

Upload::where('sponsor_id', Request::segment(2))           
        ->join('users', 'users.id', 'uploads.uploaded_by')
        ->orderBy('uploads.created_at', 'DESC')
        ->select('uploads.filename','users.first_name','users.surname',
             'uploads.errors',
            'uploads.statistics',
            'uploads.created_at')
        ->pagiate(10);

答案 1 :(得分:1)

您可以使用select方法来过滤列。然后将paginate方法放在查询的末尾。 最后,您的查询将如下所示:

Upload::where('sponsor_id', Request::segment(2))
    ->join('users', 'users.id', 'uploads.uploaded_by')
    ->orderBy('uploads.created_at', 'DESC')
    ->select(
        'uploads.filename',
        'users.first_name',
        'users.surname',
        'uploads.errors',
        'uploads.statistics',
        'uploads.created_at',
    )
    ->paginate(20);