RodrigoG's avatar

Paginate many to many relationship

Hello!. My trouble came to display all data of the pivot table with pagination in a many to many relation, in this case the pagination is not shown, no page anything, the pagination does not come out and all the records come out at once. I am somewhat new to laravel and although I have searched a lot I have not found an answer that has served me. On the other hand, with a livewire component passing a work as a parameter, the employees associated with that work page me, but not all the workers of all the works. Thanks a lot!

My code:

public function index()
{
    $employee = Employee::with('works')->paginate(5);

    return view('admin.employee_work.index',compact('employee'));
}

My view:

            @foreach ($employee as $e)    
                @foreach ($e->works as $w)
                    
                
                <tr>
                            
                  <td class="px-10 py-4 whitespace-nowrap text-sm text-gris_terciario">{{$w->pivot->id}}</td>
                  <td class="px-10 py-4 whitespace-nowrap text-sm text-gris_terciario">{{$e->name}}</td>
                  <td class="px-10 py-4 whitespace-nowrap text-sm text-gris_terciario">{{$w->code_work}}</td>
                  <td class="px-10 py-4 whitespace-nowrap text-sm text-gris_terciario">{{$w->pivot->date->format('d-m-Y')}}</td>
                  <td class="px-10 py-4 whitespace-nowrap text-sm text-gris_terciario">{{$w->pivot->number_hours_or}}</td>
                  <td class="px-10 py-4 whitespace-nowrap text-sm text-gris_terciario">{{$w->pivot->number_hours_ex}}</td>      

                </tr>                     
              @endforeach
            @endforeach
                    
          </tbody>
        </table>  
                    

            <div class="bg-gris_claro px-2 py-2">
              {{ $employee->links() }}
            </div>
0 likes
2 replies
bugsysha's avatar

Have you changed the template or anything regarding the pagination. In general the provided code should work.

RodrigoG's avatar

I think not, only what is the layout of the pagination, the tailwind css part, in fact, the livewire component pages me well, but the employees of a past work page me as a parameter

Please or to participate in this conversation.