Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 3 months ago.
0

childs [id, name, DOB]
bookings [id, child_id, club, day]

Child.php

public function bookings(){ return $this->hasMany('Booking', 'bookings'); }

Booking.php

public function child(){ return $this->belongsTo('Child', 'child_id');

$child = Child::find(1);  
$bookings = $child->bookings;

foreach($bookings as $booking) echo $booking->child->name;

Is this along the right lines?

Last updated 3 months ago.
0

Editing on these forums blows. Sorry for the above formatting issue but the line


Should be the only thing in `Booking.php` for my example
Last updated 3 months ago.
0

Thanks, thats pretty much what I had i was just calling it wrong, for some reason i thought more work would be involved to reverse it but simply this $booking->child->name; does the job... gotta love Laravel

Thanks man

Last updated 3 months ago.
0

Sign in to participate in this thread!

Cloudways

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2022 Laravel.io - All rights reserved.