Search code examples
laravelwhere-in

Argument 1 passed to Illuminate\Database\Query\Builder::cleanBindings() must be of the type array, string given, called in Error but i pass array


When i try to get posts in laravel with whereIn i get this error:

Argument 1 passed to Illuminate\Database\Query\Builder::cleanBindings() must be of the type array, string given, called in

But i passed argument as array, bellow i will post code, thanks in advance.

$arrayRecived = json_decode($request->fallowList);
var_dump($arrayRecived);
$result = DB::table('posts')->leftJoin('post_votes', function($join) {
    $recived = auth()->userOrFail();
    $join->on('posts.id', '=', 'post_votes.post_id')->where('post_votes.user_id', '=', $recived['id']);
})->select('posts.id', 'posts.user_id', 'posts.description', 'vote', 'posts.votes', 'posts.filename')
->whereIn('posts.user_id', '=', $arrayRecived)
->orderBy('posts.id', 'desc')->get();

return response()->json(['posts' => $result], 200);

If i do var_dump() on $arrayRecived after i do json_decode() it says is an array and in console is like:

array(2) {
  [0]=>
  int(3)
  [1]=>
  int(9)
}

Solution

  • whereIn() only takes 2 parameters, where the second is the array. So the following change should make it work.

    ->whereIn('posts.user_id', $arrayRecived)
    

    Bonus, if you wanted to do != equivalent it should be.

    ->whereIn('posts.user_id', $arrayRecived, 'and', true)