php - laravel eloquent ->whereHas - 编写你自己的存在(子查询)

标签 php mysql laravel eloquent exists

Laravel Eloquent ->whereHas() 使用 exists() 子查询 - https://dev.mysql.com/doc/refman/8.0/en/exists-and-not-exists-subqueries.html - 为了返回您的结果。

我想写我自己的子查询,但我不知道如何告诉 Eloquent -> 它在哪里。

如果我这样做:

$query->where( DB::raw(' exists( subquery ) ')

Laravel 将子查询写成:

where exists( subquery ) is null

所以我只是想知道可以使用什么 $query->method() 将 exists() 子查询添加到“where”语句。子查询与 laravel 生成的类型相同,但写成:

... and exists ( select * from `tbl` inner join `assets` on `custom_assets`.`id` = `tbl`.`asset_id` where `assets`.`deleted_at` is null and `users`.`id` = `assets`.`client_id` and `field_id` = ? and (`value` = ? and `assets`.`deleted_at` is null )

最佳答案

使用whereRaw() :

$query->whereRaw('exists( subquery )')

关于php - laravel eloquent ->whereHas - 编写你自己的存在(子查询),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52070297/

相关文章:

php - 使用php格式化mysql时间戳

php - 在 PHP 数组中查找最大值

mysql - JPA - 多个表中的唯一值

laravel - 在 Laravel 5.3 中访问中间件中的路由 URL 参数

php - Laravel RAW 查询比 Laravel 之外的相同查询慢几个数量级

php - 在RDBMS中实现数据匿名化时,如何正确分离用户数据?

php - 计算数组内部的数组? PHP

php - 如何使用 CodeIgniter 的 ActiveRecord 选择列值不为 NULL 的行?

mysql - ORDER BY 真值

javascript - Vue 1 类绑定(bind)始终返回 false