Pagination hasMany Laravel 5.1

4

I have the following relationship in my Model Client:

public function Usuario(){ 
    return $this->hasMany('SIST\Models\Admin\Usuario', 'id_cliente');
}

I pass the client data by my Controller :

$cliente = Cliente::find($id_cliente);
return view('admin/usuario/index',['cliente' => $cliente]);

And I list users in my View :

@foreach($cliente->Usuario as $usuario)

I need to know how to page the results.

    
asked by anonymous 22.01.2016 / 14:31

1 answer

2

Instead of calling the relationship directly, you can use the relationship method to call the paginate.

$cliente = Cliente::findOrFail($id_cliente);

$usuarios = $cliente->usuarios()->paginate(15);

In view, you will replace $cliente->usuarios with $usuarios on your foreach .

And to display the pagination links, do so on view :

  {{ $usuarios->links() }}

You can also optionally use the $usuario->render() method. These two methods serve the same purpose.

    
22.01.2016 / 15:37