0

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

5
  • 1
    they are not arrays they are Collections which are objects ... laravel.com/docs/8.x/collections#available-methods some of the available methods on the Collection class ... if you want the array from the Collections you can call all() on them Commented Oct 1, 2020 at 23:19
  • thanks for correction. and how to turn my data become an array ? Commented Oct 1, 2020 at 23:22
  • but when i use all its turn like [{ data }] and object inside the array Commented Oct 1, 2020 at 23:24
  • when i use get all its turn like [[{ data }]] Commented Oct 1, 2020 at 23:25
  • how to transform my data become array like [ data ] Commented Oct 1, 2020 at 23:25

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

Comments

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.