Hello,
I am having trouble generating a query using the Query Builder of Laravel 4.2 , the difficulty arises from the need for resulting queries in the database to be inside of parentheses, below is the desired query:
select * from 'EV_RELATORIO'
where 'DTALTERACAO' between ? and ? and
('MUN_ID' is null or ('MUN_ID' in (?))) and
('REG_ID' is null or ('REG_ID' in (?)));
But you are returning the following query:
select * from 'EV_RELATORIO'
where 'DTALTERACAO' between ? and ? and
'MUN_ID' in (?) or 'MUN_ID' is null and
'REG_ID' in (?) or 'MUN_ID' is null';
The construction of the querie within the model using Laravel follows below: (Archive VW_Relation.php )
<?php
class VW_Relatorio extends BaseModel
{
protected $table = 'EV_RELATORIO';
protected $primaryKey = 'ID';
public function scopeMundos($query, $dados) {
if(!empty($dados)){
$query->whereIn('MUN_ID', $dados)->orWhereNull('MUN_ID');
}
return $query;
}
public function scopeRegionais($query, $dados) {
if(!empty($dados)){
$query->whereIn('REG_ID', $dados)->orWhereNull('MUN_ID');
}
return $query;
}
}
I make the call in my class with the following code:
$rel = \VW_Relatorio::whereBetween('DTALTERACAO',array($dataInicial, $dataFinal))
->mundos($data[0]['mundos'])
->regionais($data[0]['regionais'])
->get();
I tried to use "Where" advanced, but I can not generate the querie correctly.
Thank you in advance for your attention.