arrayslaravelpluck

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


i know this is similar with other question, but. how if we get the data array from db and try to compare those data ?

$x = Video::where('kursus_id', $data)->pluck('slug');
$z = Video::where('kelas_id', $data_id_kelas)
    ->where('mapel_id', $data_id_mapel)
    ->pluck('slug');

foreach ($z as $value) {
    if (in_array($value, $x)) {
    
    } else {
         echo $value.'<br>';
    }
}

this throw me an error like in_array() expects parameter 2 to be array, object given.

but when i try to change $x and $z with $x=["2"] $z=["1","2","3"] Thats work. and output is 1 & 3 i think when i use db and give it pluck, this will became the same output when i use regular array like ["1","2","3"].

please correct me if my opinion goes wrong. cz im in study. Thanks before mates


Solution

  • in_array() it 2nd param req. array and you are giving object that's why this error

    to fix this you need to use toArray() function in laravel

    $x = Video::where('kursus_id', $data)->pluck('slug')->toArray();
    

    then you can use in_array($value, $x)