Skip to content

Reverse relationship with adjacency model #215

Answered by staudenmeir
denizgolbas asked this question in Q&A
Discussion options

You must be logged in to vote

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):

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();

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by denizgolbas
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants