Any help on this guys would be great :)
Laravel Has Many Though
Good Morning,
I've got 3 diffrent models booking, course, location
I want to be able to display the course name though the relationship
I have bookings that holds the location id the locations table holds the course id that then links to the course table which has the name how can I link the two together?
You want to do this?
Booking::all()->first()->course->name;
That wont work
I have no idea what you try to do, can you give an example what you want to be able to?
Normally you would do this
Booking::all()->first()->location->course->name;
You have to set up the relations of course, is this what you are missing?
I want to pull the course name but the relationship type is a Has Many Though I have this so far and its not working
public function courses()
{
return $this->hasManyThrough('App\Course', 'App\Location', 'location_id');
}
that was put in the booking model
Yes that should be working fine, even this should work
return $this->hasManyThrough(Course::class, Location::class);
To be specific then this
return $this->hasManyThrough(Course::class, Location::class, 'location_id', 'course_id');
What error do you receive?
ErrorException in Connection.php line 673: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'locations.location_id' in 'field list' (SQL: select courses.*, locations.location_id from courses inner join locations on locations.id = courses.location_id where locations.location_id = 1243) (View: /Users/danny/Code/jobskilla/resources/views/backend/users/bookings/index.blade.php)
From that error, you don't have the locations table or location_id column.
It's not called locations_id eventually?
I have 3 tables
bookings
id, location_id, user_id, created_at, updated_at
locations
id, course_id, venue_name, address ..., start_date, end_date
courses
id, company_id, title, description, tags, created_at, updated_at
I'm using the bookings model and i want to be able to get the course title that the user booked on that event
My mistake. Of course you don't have locations.location_id it's locations.id :-)
return $this->hasManyThrough(Course::class, Location::class, 'booking_id', 'location_id');
But do you have many courses per booking?
I'm sure this is really what you want to achieve
$booking->location->course->name;
I've tried following
public function course() { return $this->hasManyThrough(Course::class, Location::class, 'booking_id', 'location_id'); }
Column not found: 1054 Unknown column 'locations.booking_id' in 'field list' (SQL: select courses.*, locations.booking_id from courses inner join locations on locations.id = courses.location_id where locations.booking_id = 1243) (View: /Users/danny/Code/jobskilla/resources/views/backend/users/bookings/index.blade.php)
Yea because you don't have a booking_id anywhere. I think you are trying to build the wrong relations. As I asked before, do you really have several courses per booking? Because that's the relationship you are trying to make. I think what you have is several bookings per course, then you can have a hasManyThrough on Course to find bookings, but not the other way around.
1 course per booking
Then this is your relation:
$booking->location->course->name;
public function getLocationAttribute()
{
return Location::find($this->attributes['location_id']);
}
And the same in location but with course.
Please or to participate in this conversation.