I think you could use this logic in a method on your Post model:
class Post extends Eloquent{
...
public function country(){
return $this->user->country;
}
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
hasManyThrough (http://laravel.com/docs/eloquent#has-many-through) is a great way to define complex nested relationships for models, however, is there a reverse of it?
Using the example provided in the docs:
countries
id - integer
name - string
users
id - integer
country_id - integer
name - string
posts
id - integer
user_id - integer
title - string
You can easily bounce through the users table and return all posts for a country:
$posts = Country::posts();
However, how can you go the other way, and retrieve a country for a given post? Currently it looks like you need to do:
$country = $post->user->country;
Is there a nicer way?
I think you could use this logic in a method on your Post model:
class Post extends Eloquent{
...
public function country(){
return $this->user->country;
}
}
Please or to participate in this conversation.