eloquent 的原始查询

Raw query to eloquent

我正在将以下查询转换为 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.