0

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

1 Answer 1

2

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();
Sign up to request clarification or add additional context in comments.

7 Comments

Still gives empty array
can you show what you get in dd($r->all()); and try to use like smaller one.
[ "id" => "1047" "position" => "Care Assistant,Live In" "first_time_days" => "Friday" "second_time_days" => "Saturday" "daysit_days" => null "sleepin_days" => null "first_time_type" => "06:45-15:00" "second_time_type" => "15:00-21:45" "other_type" => "Other" "daysit_type" => "DaySit" "sleepin_type" => "SleepIn" "week_type" => "Weeks" "waking_type" => "WakingNight" "other_data" => "nonef" "week_data" => "10" "AnnualLeave" => "Yes" "AnnualLeaveDates" => "19-10-2020,21-10-2020,23-10-2020" ]
The result of dd($r->all()); no problems with that one
try this ->where('Position', 'like', "%".$r->position."%")
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.