mysql - 如何使用 Laravel Eloquent ORM 或查询生成器从 whereIn 子句中进行选择?

标签 mysql laravel eloquent

我想通过使用 Eloquent ORM 的以下 MySQL 来获取值(value)。

MySQL 查询 -

SELECT * FROM `users` WHERE `is_super_admin` = 1 OR `is_partner` = 1 
OR `is_admin` = 1 OR `id` IN 
(select `user_id` from `site_userrole_mapper` where `is_site_owner` = 1)

Laravel 查询(不起作用)-

Users::Where('is_super_admin', '=', 1)
        ->orWhere('is_partner', '=', 1)
        ->orWhere('is_admin', '=', 1)
        ->orWhereIn('id', [
            DB::table('site_userrole_mapper')
            ->select('user_id')
            ->where('is_site_owner', '=', 1)
            ->get()
            //Here get() returns an object and we're not able compare it with id
        ])
        ->get();

我只想将上面的 MySQL 查询转换为 Eloquent ORM。感谢您的帮助。

最佳答案

试试这个,

更改此查询并提取user_id

whereIn('id',[
DB::table('site_userrole_mapper')
        ->select('user_id')
        ->where('is_site_owner', '=', 1)
        ->get()
   ])

whereIn('id',
DB::table('site_userrole_mapper')
        ->where('is_site_owner', '=', 1)
        ->pluck('user_id')
      )

关于mysql - 如何使用 Laravel Eloquent ORM 或查询生成器从 whereIn 子句中进行选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44901093/

相关文章:

php - 仅获取 Yahoo Historical API 上 30 天的数据

MYSQL DateTime 查询以获取特定日期的值

mysql - 查询多个表的总和

mysql - 在 Laravel5.4 中将数据插入数据透视表

mysql - 从 Laravel 5.1 升级到 Laravel 5.8 后,whereHas() 速度变慢

mysql - 集合的交集

php - 如何在index.php文件中使用config()函数?

laravel - 使用 Homestead 销毁和重新创建 VM 的正确方法

php - 拉拉维尔 5.5 : Am I using Dependency Injection in right mode?

php - 可观察的更新值