我尝试使用下面的表单输入构建动态查询:
$query = new Doctor();
if (!empty($request->input('city'))) {
$city = $request->input('city');
//Search for any thing like doctor name or last name
$query->where('city_id', $city);
}
//Get doctor names result in collection type
$col = $query->where('level','LIKE','%%')->orderBy($Order_By, $Sort_Type)->toSql();
问题是检查城市和注入城市的部分,其中查询中的条款不起作用,我得到的查询是:
"select * from `doctors` where `level` LIKE ? order by `profile_views` desc"
如果我更改逻辑并将city where子句放在单行中,如:
$col = $query->where('city_id', $city)->where('level','LIKE','%%')->orderBy($Order_By, $Sort_Type)->toSql();
此查询的结果是:
"select * from `doctors` where `city_id` is null and `level` LIKE ? order by `profile_views` desc"
如何让第一个逻辑工作?
答案 0 :(得分:2)
$query = new Doctor();
创建一个新医生,但你并不是真的想要那个。代替:
$query = Doctor::query();
针对Doctor模型创建一个新的查询,然后可以使用where()
等内容进行扩展。