Reputation: 75
I'm using Laravel query builder to retrieve results from db but it shows me empty array but whenever i use raw query it shows me results. Any solutions?
RAW query (showing results)
$work_query = 'SELECT * FROM work_availability WHERE EmployeeID = ' . $id . ' AND Position LIKE "%' . $r->position . '%" AND Type = "' . $r->first_time_type . '"';
Laravel Query builder (return empty array)
$work_first_time = DB::table('work_availability')
->where('EmployeeID', $r->id)
->where('Position', 'LIKE', " % $r->position % ")
->where('Type', '"'.$r->first_time_type.'"')
->get()->toArray();
Upvotes: 0
Views: 582
Reputation: 15319
Try this
1.Error is in ->where('Type', '"'.$r->first_time_type.'"')
. quotes not required in laravel
2.Erorr in ->where('Position', 'LIKE', " % $r->position % ")
You can use here two ways
->where('Position', 'like', "%{$r->position }%")
or
->where('Position', 'like', "%".$r->position."%")
So Final code will be
$work_first_time = DB::table('work_availability')
->where('EmployeeID', $r->id)
->where('Position', 'like', " %{$r->position}% ")
->where('Type', $r->first_time_type)
->get()->toArray();
Upvotes: 2