Penaf's avatar
Level 1

Undefined Builder::links() when paginating many to many relation

Good evening,

while this bit on my controller works perfect:

    public function info($coleccao_tematica) {

        $coleccaoTematica = ColeccaoTematica::where('slug', 'LIKE', $coleccao_tematica)
            ->withCount('multimedia', 'especie')
            ->with('multimedia', 'poi', 'vid', 'capa')
            ->with(['especie' => function ($query) {
                $query->paginate(2);
            }])

            ->first();

        return view('coleccao_tematica', compact('coleccaoTematica'));

    }

On my blade template doing

{{ $coleccaoTematica->especie()->links() }}

doesn't generate the pagination and gives the follwing error:

Call to undefined method Illuminate\Database\Query\Builder::links() (View: /home/vagrant/Code/ecocampus/resources/views/coleccao_tematica.blade.php)

What am I missing? :/

0 likes
2 replies
staudenmeir's avatar
Level 24

You can't use pagination in eager loading:

$coleccaoTematica = ColeccaoTematica::where('slug', 'LIKE', $coleccao_tematica)
    ->withCount('multimedia', 'especie')
    ->with('multimedia', 'poi', 'vid', 'capa')
    ->first();

{{ $coleccaoTematica->especie()->paginate(2)->links() }}

Please or to participate in this conversation.