Reputation: 77
I need help to paginate a table that has a relationship with another using Laravel pagination. I get an error that says...
too fewer argument to function
Controller
public function users()
{
$users = User::join('customers', 'users.id', '=', 'customers.user_id')
->select('customers.*')->orderBy('customers.name', 'asc')
->paginate(2);
return view('paginate', ['users' => $users]);
}
View
<div class="container">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Location</th>
<th>Age</th>
</tr>
</thead>
<tbody>
@foreach ($users->customers() as $customer)
<tr>
<td>{{$customer->id}}</td>
<td>{{$customer->location}}</td>
<td>{{$customer->age}}</td>
</tr>
@endforeach
</tbody>
</table>
{{$users->customers()->links()}}
</div>
<script type="text/javascript" src="{{asset('js/frontend_js/jquery.min.js')}}"></script>
<script type="text/javascript" src="{{asset('js/frontend_js/bootstrap.min.js')}}"></script>
Relationship (between the two tables)
public function customers()
{
return $this->hasMany('App\Customer')->paginate(2);
}
public function users()
{
return $this->belongsTo('App\User');
}
Upvotes: 0
Views: 183
Reputation: 2292
Replace this line
{{$users->customers()->links()}}
with this
{{$users->links()}}
Remove pagination
from this relation.
public function customers(){
return $this->hasMany('App\Customer');
}
Upvotes: 1