Laravel 5 eloquent join基于字段最大值

时间:2015-09-25 14:27:24

标签: laravel laravel-5 eloquent

我正在尝试从users表中获取行,并将其与Articles表连接,其中articles.user_id = users.id和articles.view最多。

public function scopeMostViewedArticle($query)
{
    $query->leftjoin('articles as a', function ($join) {

            $join->on('a.user_id','=','users.id')
                  ->where('a.publish_date', '<',date('Y-m-d H:m'))
                  ->where('a.view','=',?);


            })
        ->groupby('users.id')
        ->orderby('users.id')
        ->select('users.*','a.title as article_title','a.image as article_image');  


}

我对' - &gt; where('a.view','=',?);'有疑问部分。 我感谢您的帮助! :)

1 个答案:

答案 0 :(得分:0)

public function scopeMostViewedArticle($query)
{
    $query->leftjoin('articles as a', function ($join) {

            $join->on('a.user_id','=','users.id')
                  ->where('a.publish_date', '<',date('Y-m-d H:m'))
                  ->max('a.view');   
            })
        ->groupby('users.id')
        ->orderby('users.id')
        ->select('users.*','a.title as article_title','a.image as article_image');  

}

您可以找到更多信息 http://laravel.com/docs/5.1/queries