我正在尝试使用Laravel查询生成器的JOIN查询添加条件 .
<?php
$results = DB::select('
SELECT DISTINCT
*
FROM
rooms
LEFT JOIN bookings
ON rooms.id = bookings.room_type_id
AND ( bookings.arrival between ? and ?
OR bookings.departure between ? and ? )
WHERE
bookings.room_type_id IS NULL
LIMIT 20',
array('2012-05-01', '2012-05-10', '2012-05-01', '2012-05-10')
);
我知道我可以使用Raw Expressions但是会有SQL注入点 . 我've tried the following with Query Builder but the generated query (and obviously, query results) aren' t我的意图:
$results = DB::table('rooms')
->distinct()
->leftJoin('bookings', function ($join) {
$join->on('rooms.id', '=', 'bookings.room_type_id');
})
->whereBetween('arrival', array('2012-05-01', '2012-05-10'))
->whereBetween('departure', array('2012-05-01', '2012-05-10'))
->where('bookings.room_type_id', '=', null)
->get();
这是Laravel生成的查询:
select distinct * from `room_type_info`
left join `bookings`
on `room_type_info`.`id` = `bookings`.`room_type_id`
where `arrival` between ? and ?
and `departure` between ? and ?
and `bookings`.`room_type_id` is null
如您所见,查询输出没有结构(特别是在JOIN范围内) . 是否可以在JOIN下添加其他条件?
如何使用Laravel的查询生成器构建相同的查询(如果可能)使用Eloquent更好,还是应该使用DB :: select?
6 回答
如果你有一些参数,你可以这样做 .
然后返回您的查询
返回$ results;
不太确定是否可以在laravel中添加between子句 .
Notes:
DB :: raw()指示Laravel不要放回引号 .
通过将闭包传递给连接方法,可以向其添加更多连接条件,on()将添加AND条件,而orOn()将添加OR条件 .
您可以在左连接中复制这些括号:
是
然后,您必须使用"setBindings"设置绑定,如此SO帖子中所述:How to bind parameters to a raw DB query in Laravel that's used on a model?
它不漂亮,但它的工作原理 .
The sql query sample like this
Laravel join with multiple conditions
我正在使用laravel5.2,我们可以添加不同选项的连接,您可以根据您的要求进行修改 .
原始查询和标准选择之间存在差异(在
DB::raw
和DB::select
方法之间) .您可以使用
DB::select
执行您想要的操作,只需简单地放入?
占位符,就像使用预处理语句一样(它正在执行) .一个小例子:
第二个参数是一个值数组,用于从左到右替换查询中的占位符 .