Laravel 5: in_array() expects parameter 2 to be array, object given

You can convert the collection to an array like other suggested using toArray() method. But i'd rather change

in_array($post->id, $votes)

to

$votes->contains($post->id)

Update

The above only works if $votes is a collection of IDs. Eg. when you use

$votes = Auth::user()->votes()->pluck('post_id');

However, with your current controller code you would need to use

$votes->contains('post_id', $post->id);

since $votes is a collection of Vote objects, and you only want to search in the post_id column/attribute.


You need to convert your collection to array. Just append toArray() to your query like below, as 'toArray()' converts the collection into a plain PHP array:

 $votes = Auth::user()->votes()->get()->toArray(); 

Hope this help.