简体   繁体   English

如何使用orWhere创建动态where子句-Laravel 4

[英]How to use orWhere to create dynamic where clause - Laravel 4

how I can to write this in Eloquent and I'm uisng laravel 4? 我该如何用Eloquent和uisng laravel 4编写此代码?

SELECT * FROM `applicant` WHERE `id`='17' OR `id`='19'

I am thinking like this 我这样想

$findIDAUTH = Auth::User()->id;
$findHrsbu = HrSbu::where('staff_id', '=', $findIDAUTH)->get();
$results = Mapplication::MApplication();
foreach($findHrsbu as $HrSbu){
    $results = $results->Where('sbu.id', '=', $HrSbu->sbu_id);
}
$results = $results->get();

if just 如果只是

SELECT * FROM `applicant` WHERE `id`='17'

i'ts work but if 我工作,但是如果

SELECT * FROM `applicant` WHERE `id`='17' OR `id`='19' or 'id'='20'

not working for me thanks 不为我工作谢谢

You should use whereIn with a subquery: 您应该将whereIn与子查询一起使用:

$results = Mapplication::whereIn('id', function ($query) {
    $query->from((new HrSbu)->getTable())
          ->select('sbu_id')
          ->where('staff_id', auth()->id());
})->get();

Hi Please try with this code and let me know, here is an option by laravel as you used its a similar method.... 嗨,请尝试使用此代码,让我知道,这是laravel的一个选项,因为您使用了类似的方法。

$findIDAUTH  = Auth::User()->id;
$findHrsbu   = HrSbu::where('staff_id', '=', $findIDAUTH)->get();
$results =      Mapplication::MApplication()->where(function($findHrsbu)->where(function($results) use ($findHrsbu) {
foreach($findHrsbu as $HrSbu){ 
   $results->orWhere('sbu.id', '=', $HrSbu->sbu_id);
}
})->get();

嗨,伙计们感谢您的回答。解决了问题,我正在使用WhereIn作为示例: $results->whereIn('sbu.id', '=', array($dataArray));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM