使用orWhere和Join的Eloquent查询出错Laravel

wwwo4jvm  于 2023-03-31  发布在  其他
关注(0)|答案(1)|浏览(129)

你好,我有这个查询,我使用orWhereJoin但不工作。这是我的查询:

$hasBorrowedCars = CarBorrow::query()
            ->where('borrowed_from_user_id', $userId)
            ->orWhere(function ($query) use ($userId) {
                $query->join('car_owners', 'user_id', '=', 'car_owners.user_id')
                    ->where('user_id', $userId);
            })
            ->exists();

这是我得到的错误:

Column not found: 1054 Unknown column 'user_id' in 'where clause' (SQL: select exists(select * from `car_borrows` where `borrowed_from_user_id` = 2 or (`user_id` = 2)) as `exists`)
jvlzgdj9

jvlzgdj91#

join子句中缺少表名。假设表名为car_borrows

$hasBorrowedCars = CarBorrow::query()
    ->where('borrowed_from_user_id', $userId)
    ->orWhere(function ($query) use ($userId) {
        $query->join('car_owners', 'car_borrows.user_id', '=', 'car_owners.user_id')
            ->where('car_borrows.user_id', $userId);
    })
    ->exists();

相关问题