繁体   English   中英

我如何急于加​​载最新记录,其中每个记录的某个列具有不同的值?

[英]How do I eager load the latest record where each record have a different value of a certain column?

比方说,我有这个表叫做邮件

+----+--------------+-------------+-----+
| ID | user_id      | conv_id     |body |
+----+--------------+-------------+-----+
|1..5|            1 |           1 | ... |
+----+--------------+-------------+-----+
| 6  |            1 |           3 | ... |
+----+--------------+-------------+-----+
|  7 |            1 |           3 |...  |
+----+--------------+-------------+-----+
|  8 |            1 |           1 |...  |
+----+--------------+-------------+-----+
|  9 |            1 |           2 |...  |
+----+--------------+-------------+-----+
| 10 |            1 |           1 | ... |
+----+--------------+-------------+-----+
| 11 |            1 |           2 |...  |
+----+--------------+-------------+-----+ 
| 12 |            1 |           4 |...  |
+----+--------------+-------------+-----+
| 13 |            1 |           5 |...  |
+----+--------------+-------------+-----+
| 14 |            1 |           4 |...  |
+----+--------------+-------------+-----+

我想输出这个结果:

+----+--------------+-------------+-----+
|2..5|            1 |           1 | ... |
+----+--------------+-------------+-----+
| 10 |            1 |           1 | ... |
+----+--------------+-------------+-----+
|  7 |            1 |           3 | ... |
+----+--------------+-------------+-----+
| 11 |            1 |           2 | ... |
+----+--------------+-------------+-----+
| 13 |            1 |           5 |...  |
+----+--------------+-------------+-----+
| 14 |            1 |           4 |...  |
+----+--------------+-------------+-----+

渴望加载的结果。 如您所见,它将输出5条最新记录,其中conv_id = 1 加上每隔一个conv_id最新记录 我怎么做?


这是我的控制器代码,也是我尝试过的

$loginuser = User::find(Auth::user()->id);

//$allOpenConvMsgs is an array of 5 message IDs where `conv_id = 1`.
//The relationship between user and messages is many-to-many. 
  See model codes below

$newMessages = $loginuser->messages()->where(function($q){
                   $q->where(''); //I'm stuck here!
               })->orWhereIn('messages.id',$allOpenConvMsgs);

消息模型

public function user(){
    return $this->belongsToMany('User');
}

用户模型

public function messages(){
    return $this->belongsToMany('Messages');
}

如果我明白您的意思,您可以执行以下操作:

$loginuser = User::find(Auth::user()->id)->with(array('messages' => function($q){
    $q->where('conv_id', '=', '1')->take(5)->orderBy('id', 'desc');
})->get();

$newMessages = $loginuser->messages;

更新

在David的建议之后,最好通过Auth::user()函数使用已获取的User实例。

因此,上面的代码将是:

$newMessages = Auth::user()->load(array('messages' => function($q){
        $q->where('conv_id', '=', '1')->take(5)->orderBy('id', 'desc');
    }))->messages;

更新2

上面的代码仅获取conv_id = 1的消息,以获取您必须除去where条件的所有最新消息:

$newMessages = Auth::user()->load(array('messages' => function($q){
            $q->take(5)->orderBy('id', 'desc');
        }))->messages;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM