I'm trying to build dynamic query using form inputs like below :
$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();
Problem is the part which check city and inject city where clause into query is not working and what I get as a query is :
"select * from `doctors` where `level` LIKE ? order by `profile_views` desc"
if I change the logic and put city where clause in single line like :
$col = $query->where('city_id', $city)->where('level','LIKE','%%')->orderBy($Order_By, $Sort_Type)->toSql();
The result of this query is :
"select * from `doctors` where `city_id` is null and `level` LIKE ? order by `profile_views` desc"
how can I make the first logic to work ?
$query = new Doctor();
creates a new Doctor, but you don't really want that. Instead:
$query = Doctor::query();
creates a new query against the Doctor model, which can then be extended with where()
etc. stuff.