我有一个接受原始 where 条件和连接的函数:
query('data',['fieldA','fieldB'], 'fieldA > 10 AND fieldB < 20', 'LEFT JOIN users ON data.user_id = users.id');
function query($table, $keys = [], $where = '', $joins = '') {
$query = DB::table($table)->select($keys);
if(!empty($where)) {
$query=$query->whereRaw($where);
}
if(!empty($joins)) {
$query=$query->?????????????
}
return $query->get();
}
我如何像使用 whereRaw 来处理 where 条件那样,将原始连接与查询构建器一起使用?
最佳答案
没有好闻但工作
\DB::table('data')->join('user', function($join){
$join->on(\DB::raw('( `data`.`user_id` = `user.id` or (`data`.`user_id` is null and `data`.`other_field` is null)) and 1 '),'=',\DB::raw('1'));
})
关于php - laravel 原始连接查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46566920/