Laravel 4:在嵌套关系中,我如何急切加载并检索第一个/最后一个数据?

Mar*_*ark 1 php laravel eloquent laravel-4

A : User,
B : Conversations,
C : Messages
Run Code Online (Sandbox Code Playgroud)

我希望 A中的A-> B-> C关系中的最新消息一起进行对话.

模型中的工作关系

User belongsToMany Conversation
Conversation belongsToMany User
Conversation hasMany Message
Message belongsTo Conversation
User hasMany Message
Message belongsTo User

**note: User and Conversation are related to a many2many relationship connected 
by a pivot table
Run Code Online (Sandbox Code Playgroud)

我试过了

$user->load(['conversations' => function($q)
            {
                $q->orderBy('created_at', 'desc');
                $q->with('Messages')->orderBy('created_at','desc');
            }]);
Run Code Online (Sandbox Code Playgroud)

它几乎工作但它没有得到最后的消息.相反,它会加载属于该对话的所有消息.

来自上述代码的不需要的结果

array (size=7)
  'id' => int 90
  'name' => string 'haha,hehe' (length=9)
  'created_at' => string '2014-07-03 15:04:04' (length=19)
  'updated_at' => string '2014-07-03 15:04:04' (length=19)
  'microseconds' => string '1404370893302' (length=13)
  'pivot' => 
    array (size=2)
      'user_id' => int 1
      'conversations_id' => int 90
  'messages' => 
    array (size=27)
      0 => 
        ...
      1 => 
        ...
      ...
      26 => 
          array (size=7)
             'id' => int 816
             'user_id' => int 1
             'conv_id' => int 90
             'body' => string 'test1' (length=5)
             'created_at' => string '2014-07-03 01:13:35' (length=19)
             'updated_at' => string '2014-07-03 01:13:35' (length=19)
             'microseconds' => string '1404350015499' (length=13)
Run Code Online (Sandbox Code Playgroud)

预期结果

array (size=7)
      'id' => int 90
      'name' => string 'haha,hehe' (length=9)
      'created_at' => string '2014-07-03 15:04:04' (length=19)
      'updated_at' => string '2014-07-03 15:04:04' (length=19)
      'microseconds' => string '1404370893302' (length=13)
      'pivot' => 
        array (size=2)
          'user_id' => int 1
          'conversations_id' => int 90
      'messages' => 
        array (size=1)
          0 => 
             array (size=7)
               'id' => int 816
               'user_id' => int 1
               'conv_id' => int 90
               'body' => string 'test1' (length=5)
               'created_at' => string '2014-07-03 01:13:35' (length=19)
               'updated_at' => string '2014-07-03 01:13:35' (length=19)
               'microseconds' => string '1404350015499' (length=13)
Run Code Online (Sandbox Code Playgroud)

Jar*_*zyk 5

当您在闭包中放置get(或first/ findetc)时,它再次运行查询,但忽略该查询,然后Eloquent再次执行该查询:

$user->load(['conversations' => function($q)
{
   $q->orderBy('created_at', 'desc');
   $q->with('Messages')->orderBy('created_at','desc')
     ->get()  // this query is executed here
     ->first();  // this calls first method on the collection
     // but those 2 lines above take no effect with the eager load models
     // because Eloquent will execute the query again in the end
}]);
Run Code Online (Sandbox Code Playgroud)

这是你如何在嵌套关系上设置约束(但它不会在这里完成工作):

User::with(['conversations.messages' => function ($q) {
  $q->orderBy(...)->where(...) ...;
  // but limit(1) can't be used here
}])->get();
Run Code Online (Sandbox Code Playgroud)

Limit不能使用,因为它将整个集限制为1,因此总共只返回1条消息.也就是说,只有一个会话会在一个集合中加载单个消息.完全错误


因此实现这一目标的唯一方法Eloquent是这种"帮助"关系:

// Conversation model
public function latestMessage()
{
  return $this->hasOne('Message')->latest();
}

// then simply:

$user->load('conversations.latestMessage');

$user->conversations->first()->latestMessage; // single Message model
Run Code Online (Sandbox Code Playgroud)