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

时间:2015-12-15 04:47:49

标签: php laravel eloquent

我怎么能在Eloquent和我的laisvel 4中写这个?

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

我在想这个

$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();

如果只是

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

我工作但是如果

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

不为我工作谢谢

3 个答案:

答案 0 :(得分:2)

您应该将whereIn与子查询一起使用:

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

答案 1 :(得分:1)

您好请尝试使用此代码并告诉我,这是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();

答案 2 :(得分:0)

Hy家伙感谢您的回答.. 问题解决了我正在使用WhereIn 例如:$results->whereIn('sbu.id', '=', array($dataArray));