any one
Feb 13, 2017
17
Level 13
How to use paginate with relationship?
I want to use paginate. Like a user have many subscriptions and then show all subscriptions in his account using using foreach but problem is that how can paginate them here is my controller
public function fdetail($reg_no = null)
{
$s = Student::where('reg_no', $reg_no)->with('subscriptions')->first();
return view('fee.invoice_deatil',compact('s'));
}
my Subscription model
public function students()
{
return $this->belongsTo(Student::Class,'student_id');
}
my Student model
public function subscriptions()
{
return $this->hasMany(Subscription::Class)->latest();
}
my view
@foreach($s->subscriptions as $invoi)
<tbody>
<tr>
<td>{{$invoi->id }}</td>
<td>{{ $invoi->created_at->format('d/m/Y') }}</td>
<td>{{ $invoi->month->format('F') }}</td>
</tr>
</tbody>
@endforeach
Level 16
You can try something like
Subsciption::whereHas('Student'=>function($query) use($reg_no){
$query->where('reg_no', $reg_no);
})->paginate(10);
1 like
Please or to participate in this conversation.