How to do pagination in a table that is related Laravel 5.4

1

There are 2 tables and a pivot where it makes the ManyToMany relationship between 2 tables .

A table is called Postagems and the other table is named Departamentos , so I'm trying to do a related search by department, when I do the search it takes the department data with the articles from the posting table and returns it to me , thus becoming my search.

See:

public function searchDepartamentoGet($id)
{
    $data = array(
        'titulo'        =>  'Artigo Por Departamento',
        'departamento'  =>  Departamento::with('postagems')
                                       ->where('id',  $id)
                                       ->paginate($this->departamento)
    );

    return view('frontend.artigo.departamentos' , $data);
}

Then this search returns this result:

  

The problem is that my pagination only does pagination on the variable departamento that I actually want it to paginate on the related table postagems where there are 3 articles .

How could you make this paging?

    
asked by anonymous 01.10.2017 / 00:00

1 answer

0

Well I found an easier way to do a paging with related tables. See:

 'departamento'  =>  Departamento::find($id)->postagems()->paginate($this->departamento)

This alone solves the problem.

    
01.10.2017 / 02:38