これは、laravelでgroupByMaxDateを取得するための最も簡単で最短の方法です。 user_id
でグループ化された最大日付の行を取得します 。
$rows = DB::table('papers')
->select(DB::raw('id, max(paper_update) as year,user_id'))
->groupBy('user_id')
//->orderBy('paper_update', 'desc')
->get();
このlaravelクエリは、次のようなmysqlクエリを作成します
select id, max(paper_update) as year from `papers` group by `user_id`;
出力はになります
Array
(
[0] => stdClass Object
(
[id] => 4
[year] => 30-5-2013
[user_id] => 1
)
[1] => stdClass Object
(
[id] => 5
[year] => 30-5-2013
[user_id] => 2
)
[2] => stdClass Object
(
[id] => 6
[year] => 30-5-2014
[user_id] => 3
)
[3] => stdClass Object
(
[id] => 7
[year] => 30-5-2014
[user_id] => 4
)
[4] => stdClass Object
(
[id] => 10
[year] => 30-5-2016
[user_id] => 5
)
)