guyinpv
1 month ago

hasOneThrough confusion

Posted 1 month ago by guyinpv

Simplified three tables: User (id, name, car_id) Car (id, name, maker_id) Maker (id, name)

On the Maker model, I used a hasManyThrough to grab all the users with that make of car, worked just fine. return $this->hasManyThrough(User::class, Car::class, 'maker_id', 'car_id')

But I also want to go the other way. If I have a user, I should be able to read who the maker is since they can only have one car, it's kind of a direct relationship that way. It doesn't work though: return $this->hasOneThrough(Maker::class, Car::class, 'maker_id', '?????')

I'm not sure how the syntax should work in this case, because it's the User that has a car_id column, not the Car model that has a user_id column. I don't know what would go in the method parameters. I've tried a bunch of different arrangements but it doesn't return the model.

So what's the easiest way to have my User model return the maker by traversing through the Car model?

Please sign in or create an account to participate in this conversation.