Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

gnabin01's avatar

Modified Has-Many-Through Relation

I have three table user , profile and posts.user and profile has one to one relation.user and posts has one to many relations.Now how can i set relation from posts to profile directly??. I was thinking about has many through relation but not success according to definition of has-many-through relation. Thanks in advance!!!

0 likes
1 reply
bobbybouwmann's avatar

There is no way to do this as far as I know with a relation. You should fetch the user from the profile and then call the posts from the user

$profileWithPosts = Profile::with('user.posts')->where('id', $id)->first();

$user = $profileWithPosts->user;
$posts = $profileWithPosts->user->posts; // Or $user->posts;

Please or to participate in this conversation.