Skip to content

[5.2] Lazy Eager Loading issue in LengthAwarePaginator collection #14468

@vigneshgurusamy

Description

@vigneshgurusamy

After upgrading to Laravel 5.2.41, I'm getting Method load does not exist exception while trying to call load() method in LengthAwarePaginator collection.

I found out Eloquent Builder.php paginate() method sets empty array if total count of the result set is zero. So LengthAwarePaginator converts the empty array into \Illuminate\Support\Collection which does not have load() method.

public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
    {
        $page = $page ?: Paginator::resolveCurrentPage($pageName);

        $perPage = $perPage ?: $this->model->getPerPage();

        $query = $this->toBase();

        $total = $query->getCountForPagination();

        $results = $total ? $this->forPage($page, $perPage)->get($columns) : [];

        return new LengthAwarePaginator($results, $total, $perPage, $page, [
            'path' => Paginator::resolveCurrentPath(),
            'pageName' => $pageName,
        ]);
    }

Kindly return Eloquent empty collection instead of an empty array, so eager loading won't fail.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions