Skip to content Skip to sidebar Skip to footer

Convert SQL To Eloquent To Join Multiple Table And Count

I have been this SQL working fine, but trying to convert it to Eloquent format, it keeps returning a wrong, SQL entirely which flags error. Coding with Laravel 5.5 Select arm_arti

Solution 1:

Try with this:

DB::table('arm_articles')
        ->leftJoin('arm_article_views', 'arm_articles.article_id', '=', 'arm_article_views.view_article_id')
        ->leftJoin('arm_article_likes', 'arm_articles.article_id', '=', 'arm_article_likes.liked_article_id')
        ->leftJoin('arm_article_comments', 'arm_articles.article_id', '=', 'arm_article_comments.comment_article_id')
        ->groupBy('arm_articles.article_id', 'article_topic')
        ->select(
            DB::raw('count(arm_article_views.view_article_id) as TotalViews'),
            DB::raw('count(arm_article_likes.liked_article_id) as TotalLikes'),
            DB::raw('count(arm_article_comments.comment_article_id) as TotalComments'),
            'arm_articles.article_id',
            'article_topic'
        )->get();

Post a Comment for "Convert SQL To Eloquent To Join Multiple Table And Count"