Why Laravel query returns empty array?

ZaIn Ul AbiDeen Sān

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();
John Lobo

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();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related