我有模型消息:
protected $fillable = ['id','text','girl_id','date'];
如何按日期为每个girl_id选择最后一条消息?
我的代码:
$messages=Message::select(['id','text','girl_id','date'])
->groupBY('girl_id')
->orderBY('date')
->take(1)
->get();
答案 0 :(得分:1)
只需尝试此解决方案
$messages=Message::select(['id','text','girl_id','date'])
->orderBY('date','desc')
->groupBY('girl_id')
->get()
->take(1);
答案 1 :(得分:1)
获取每个girl_id的最新消息
$messages=Message::select(['id','text','girl_id','date'])
->orderBY('date','desc','girl_id')
->get()
->unique('girl_id')