Show some code, how have you set up the relationship and how are you accessing it?
Has Many Through Relationship changes foreign key
Dear Laravel Folks,
I'm having a weird issue with one of my database relationships. Here's the setup:
Bookings table:
id, user_id, space_id, comment
1, 1, 1, Great space!
2, 2, 2, Awesome project.
Spaces table:
id, user_id, title
--
1, 1, Space 1
2, 2, Space 2
Users table:
id, email
--
1, user1@test.com
2, user2@test.com
Now, every User can make a booking. So a booking belongsTo a User.
However, users also own spaces, which can be booked. Hence, a user hasManyThrough booking-requests.
The relationship works fine in Laravel, but here's the strange thing.
If I query all the booking-requests, all of the bookings that are returned have the same user_id as the querying user.
So the booking with id 2 from the example above, would have a user_id of 1, whenever I do `$user1->booking-requests'.
Any idea, why this happens or how I can solve this issue?
Your help is very much appreciated.
I was now able to "fix" the error.
As I suspected, the MySQL query run by Laravel overwrote the user_id of the bookings table with the user_id of the spaces table.
I could solve the issue, by renaming user_id in bookings to renter_id.
Interestingly, there's now both a user_id and a renter_id on the collection models returned by the hasManyThroughrelationship, which is a bit unfortunate.
Please or to participate in this conversation.