Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years 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 2 years 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 2 years 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 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

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

Your logo here?

Laravel.io

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

© 2024 Laravel.io - All rights reserved.