Reverse relationship with adjacency model #215
Answered
by
staudenmeir
denizgolbas
asked this question in
Q&A
-
I have recursive User model and Post model. Post -> belongs to -> User Is there a way to make this relation: PostUserAncestors ? |
Beta Was this translation helpful? Give feedback.
Answered by
staudenmeir
Dec 4, 2023
Replies: 2 comments
-
When I try with hasManyDeepFromRelationsWithConstraints i get: RuntimeException Ancestors can only be at the beginning of deep relationships at the moment. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Hi @denizgolbas, You can use this query as a workaround (does not work on MariaDB): class User extends Model
{
public function recursivePosts()
{
return $this->hasManyOfDescendantsAndSelf(Post::class);
}
}
$post = Post::find($id);
$users = User::whereHas('recursivePosts', function ($query) use ($post) {
$query->where('posts.id', $post->id);
})->get(); |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
denizgolbas
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @denizgolbas,
It's not possible to define a relationship like this at the moment.
You can use this query as a workaround (does not work on MariaDB):