我想连接两个表并过滤连接表中的字段。我并不认为实际的表格在这个问题中很重要,但它是一个表格,日期与一个包含事件信息的表格相连,因此1个事件可能有更多日期。 我提出了这个雄辩的说法:
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
您会看到' where("已批准",true)'导致'其中.....和approved
=?)'问号来自哪里?我尝试了不同的东西,例如' 1',1,' True',True,true,' true' ...一切都以问号标记回来。
有什么建议吗?
谢谢!
欧文
答案 0 :(得分: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());
使用占位符和值代替占位符来获取查询。