aarad

Ho to sorting related articles based on common tags?

Posted 1 week ago by aarad

I have an articles and tags table. Every article can have multi tags. I want to get related articles for an article and sort result based on common tags. I use this code to get related articles but i don't know how to sort result based on common tags.

    $relatedArticle = Article::whereHas('tags', function($query) use ($article){
            $query->whereIn('name', $article->tags->pluck('name'));
        })->where('id', '!=', $article->id)->take(10)->get();

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.