Correct, in my case it's:
User: return $this->belongsTo(Car::class);
Car: return $this->belongsTo(Maker::class);
The idea is that a user has one car and thus also one maker, but a maker could belong to many cars, and the same car could belong to many users. Just like a user can own a Honda Accord, but other users might also own Honda Accords. And an Accord is only made by Honda. So the user could only have the maker of Honda as well.
I want to grab these in reverse too. If I have Honda, I want all the users. If I have Accord, I want all the users too.
I am perfectly able to get all the users who own Hondas:
Maker: return $this->hasManyThrough(User::class, Car::class, 'maker_id', 'car:id');
So in your example, in my User model if I want to directly grab their maker, this is what I ended up having to do yesterday.
User:
public function maker()
{
if ($this->car) {
return $this->car->maker->first();
} else {
return null;
}
}
I'm using the car() method on the user model to first check if they even have a car at all, and only then use the maker() method on the car() model to pull the data. If I don't do it this way and the user doesn't have a car, I get null object errors all over the place.
In the view, I want to maintain a consistent way of accessing the data.
For example when I want to grab the name of the car, it's pretty simple:
{{optional($user->car)->name}}
So likewise I want to have a consistent way to access the maker too:
{{optional($user->maker)->name}}
But this proves difficult. Given the maker() function I just posted, I have to write it like this, using the extra parenthesis around the maker function:
{{optional($user->maker())->name}}
I don't want to have to use parenthesis on maker while not having to use it on car. It just leads to confusion.