php - 原始查询到 Eloquent

标签 php mysql laravel eloquent laravel-query-builder

我正在将以下查询转换为 Eloquent :

select * from db.users
    Where (linker is null) and (admin_published is null)
union
select * from db.users
    Where (linker is not null) and (linker_verified = 2) and (admin_published is null)

Eloquent :

\App\User::where('linker', null)->
Where('admin_published', null)->
orWhere('linker','!=',null)->where('linker_verified', '=',2)->
orWhere('admin_published','!=', 1)->get();

它没有给我linker不为空的记录。

最佳答案

Laravel 为您提供了 Eloquent 方法 union()。你试过这个吗?您可以使用 whereNull('column')whereNotNull('column')

$first_query = \App\User::whereNull('linker')
    ->whereNull('admin_published');

$users = \App\Users::whereNotNull('linker')
    ->where('linker_verified', '=', 2)
    ->whereNull('admin_published')
    ->union($first_query)
    ->get();

Lara docs .

关于php - 原始查询到 Eloquent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45825243/

相关文章:

php - Laravel 事件不起作用

javascript - 在JS中读取CakePHP Cookie值

javascript - Vue.js 组件不工作

php - 某些页面上的页眉对齐问题?

mysql - 连接两个子查询别名

php - 从 MySQL 填充 PHP 数组以供 Amcharts 使用

c++ - 使用mysql加载数据infile相对路径错误

php - 500 内部错误 : Failed to load resource Laravel 5. 0

php - 使用 Yii2 将 Android 表情符号从 Firebase 保存到 MySQL

php - MySQL 使用 assoc 数组更新同一查询中的多个用户行