Innn Innn - 9 months ago 34
PHP Question

Why return of get() function in laravel is not array when check with is_array

I am just very new to laravel and today I have run some code and get stuck at this code.

$topic = topic::where('id', $id)->get();
echo 'yes';
echo 'no';

The result is 'no' but I can "foreach" loop through each topic.

As I unserstand, 'Foreach' accepts only array parameter but if it's a real array why is_array return "no"?


Because it's not an array. It's a collection.

You can convert it to an array with toArray() method:

$topic = topic::where('id', $id)->get()->toArray();