2017-03-13 35 views
0

我想连接两个表并过滤连接表中的一个字段。我不认为这个问题中的实际表格很重要,但它是一个表格,其中的日期与表格中的事件信息一起加入,所以1个事件可能有更多的日期。 我做了这个雄辩行:Laravel雄辩可能的错误?

Event_date::whereRaw('startdate >= curdate() OR enddate >= curdate()')->whereHas('Event', function($q){$q->where("approved",true);})->orderBy('startdate', 'asc')->orderBy('enddate', 'asc')->toSql()); 

过滤器不工作,虽然。所以这就是为什么我将 - > toSql()添加到行。 我得到以下回:

select * from `event_dates` where startdate >= curdate() OR enddate >= curdate() and exists (select * from `events` where `event_dates`.`event_id` = `events`.`id` and `approved` = ?) order by `startdate` asc, `enddate` asc 

你看“阿凡(简称‘认可’,真)”的结果“其中.....和和approved =)?”哪里的问号来自???我尝试了不同的东西,比如'1',1,'真',真,'真''......一切都回来了,成为一个问号。

有什么建议?

谢谢!

Erwin

+0

PDO的工作原理就是这样!它发送'?'占位符和一组相应的值。这是为了防止sql注入 –

回答

3

这是预期的行为。 Laravel使用准备好的语句。要获得放入占位符参数,你可以使用

$query->getBindings(); 

因此,例如,你的情况,你可以使用:

$query = Event_date::whereRaw('startdate >= curdate() OR enddate >= curdate()')->whereHas('Event', function($q){$q->where("approved",true);})->orderBy('startdate', 'asc')->orderBy('enddate', 'asc')); 

现在

echo $query->toSql(); 
var_dump($query->getBindings()); 

得到既查询占位符和值将代替占位符。