Skip to content
Advertisement

How to use like with whereIn in laravel

i am using laravel 8 and want use like query with whereIn. it’s my query:

       $details = explode(" ", $request->detail);
        $doctors->whereIn('specialities.title', 'like', '%' . $details . '%')
            ->orWhereIn('users.first_name','like',$details."%")
        ->orWhere('users.last_name','like',$details."%");

but i get errors:
{message: “Array to string conversion”, exception: “ErrorException”,…}

exception: “ErrorException” line: 39 message: “Array to string conversion”

Advertisement

Answer

I changed my logic and it work for me and return correct result: because i am searching records from multiple fields

$details = explode(" ", $request->detail);
        $doctors->where('specialities.title', 'like', '%' . $details[0]. '%')
            ->orWhere('users.first_name','like',count($details) > 1 ? $details[0] : 
         $request->detail ."%")
            ->orWhere('users.last_name','like',count($details) > 1 ? $details[1] : 
           $request->detail ."%");
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement