原始查询以雄辩

时间:2017-08-22 19:10:00

标签: 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)

雄辩:

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

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

1 个答案:

答案 0 :(得分:1)

Laravel为您提供了雄辩的方法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