【问题标题】:Laravel 5 eloquent join based field max valueLaravel 5 雄辩的基于连接的字段最大值
【发布时间】:2015-12-23 10:03:46
【问题描述】:

我正在尝试从 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');  


}

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

【问题讨论】:

  • 我在 where 子句中看不到,但在查询本身中,您选择最大值 SELECT MAX(column_name) FROM table_name;

标签: laravel laravel-5 eloquent


【解决方案1】:
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

【讨论】:

  • 感谢您的回复。我以前试过,但它说Call to undefined method Illuminate\Database\Query\JoinClause::max()
猜你喜欢
  • 1970-01-01
  • 2014-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-03
  • 2015-05-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多