I am building an application which has a model with one to many relationship. In the model, the student table has one to many relationship with student address details. I want to retrieve the last row from address details table. I am stuck on how to retrieve that data. I could not work out from similar answer on this website.
My current solution is this
$students = Student::with('visaDetails', 'addresses', 'instituteDetails', 'subAgents',
'staffDetails', 'commissionDetails', 'comments')->paginate(16);
foreach ($students as $student){
foreach ($student->addresses as $address){
dd($address->id);
}
}
My student model is as follow
class Student extends Model
{
//One to Many relation to student address
public function addresses(){
return $this->hasMany('App\Model\Address');
}
}
But this solutions is not efficient as number of record grows exponentially. Also, I need to find last row for other many table as well.
If anybody could help me then it would be very nice.