如果在子查询中加入,雄辩地找不到列

时间:2019-07-10 09:16:45

标签: join eloquent subquery

使用以下查询查询数据库时:

Album::select('album.*')
    ->where(function ($query) use ($userId) {
        $query
            //->select('album.*') <- also tried this but didn't help
            ->join('accountuser_album', 'album.id', '=', 'accountuser_album.album_id')
            ->join('accountuser', 'accountuser_album.accountuser_id', '=', 'accountuser.id')
            ->where('accountuser.user_id', $userId);
    })
    ->where(function ($query) use ($userId) {
        //Otherqueries
    })
    ->paginate($this->pageLimit, ['*'], 'page', $this->pageNumber);

我收到以下错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'accountuser.user_id' in 'where clause' (SQL: select count(*) as aggregate from `album` where (`accountuser`.`user_id` = 2))

将联接移出子查询是可行的,但对我而言却不起作用,因为我需要多个具有不同联接的子查询

Album::select('album.*')
    ->join('accountuser_album', 'album.id', '=', 'accountuser_album.album_id')
    ->join('accountuser', 'accountuser_album.accountuser_id', '=', 'accountuser.id')
    ->where(function ($query) use ($userId) {
        $query->where('accountuser.user_id', $userId);
    })
    ->where(function ($query) use ($userId) {
        //Otherqueries
    })
    ->paginate($this->pageLimit, ['*'], 'page', $this->pageNumber);

我在这里做错什么了吗?

0 个答案:

没有答案