首页 文章

Laravel Eloquent中的多个嵌套查询

提问于
浏览
0

我有一个聊天应用程序,我需要使用雄辩的ORM过滤那些在组最后一条消息中有附件[s]的用户对话 . 我有以下数据库表/模型:

  • 群组和用户 . 关系是它们之间的N-N,所以现在有's a pivot table between groups and users, named group_user. Although this table has been expanded with some extra fields, as a result group_user have it'自己的模型 .

  • 消息 . 两组和用户的关系均为1-N .

  • 附件 . 消息的关系是1-N .

此过滤的算法不是问题 - 将用户(因为我需要特定的用户组)和他的组,查找所有组消息,按创建日期按降序对其进行排序,然后选择最顶层的消息 . 然后检查此消息是否在“附件”表中有任何记录 . 很明显,我确实拥有各自模型中定义的所有关系 . 现在我有这个代码,但它返回任何消息中包含附件的所有组 . 此外,我需要按最后一条消息'created_at'日期对查询进行排序 .

return $user->user_groups()->whereHas('group', function($query) {
        $query->whereHas('messages', function ($query) {
            $query->whereHas('attachments');
        });
})->with('group')->latest()->paginate($this->group->getPerPage());

编辑 .

class User
{
  public function messages()
  {
    return $this->hasMany('App\Models\Message');
  }

  public function user_groups()
  {
    return $this->hasMany('App\Models\GroupUser');
  }
}

class Message
{
  public function attachments()
  {
    return $this->hasMany('App\Models\Attachment'); 
  }

  public function author()
  {
    return $this->belongsTo('App\Models\User');
  }

  public function group()
  {
    return $this->belongsTo('App\Models\Group');
  }
}

class Attachment
{
  public function message()
  {
    return $this->belongsTo('App\Models\Message');
  }
}

class GroupUser
{
  public function user()
  {
    return $this->belongsTo('App\Models\User');
  }

  public function group()
  {
    return $this->belongsTo('App\Models\Group');
  }
}

class Group()
{
  public function messages()
  {
    return $this->hasMany('App\Models\Message');
  }

  public function group_users()
  {
    return $this->hasMany('App\Models\GroupUser');
  }
}

1 回答

  • 0

    User 模型添加 groups 关系:

    public function groups() {
        return $this->belongsToMany('App\Models\Group');
    }
    

    然后尝试这个:

    $user->groups()
        ->select('groups.*')
        ->join('messages', function($join) {
            $join->on('groups.id', 'messages.group_id')
                ->where('created_at', function($query) {
                    $query->selectRaw('max(created_at)')
                        ->from('messages')
                        ->where('group_id', DB::raw('groups.id'));
                });
        })
        ->whereExists(function($query) {
            $query->from('attachments')
                ->where('attachments.message_id', DB::raw('messages.id'));
        })
        ->orderByDesc('messages.created_at')
        ->paginate($this->group->getPerPage());
    

相关问题