Iterate through a result set in a Laravel controller

I am trying to just iterate over the result set in laravel on the controller side. This is what I tried but get the following error:

Cannot use object of type stdClass as array 

Controller Debugger:

 $result = DB::select($query); foreach($result as $r){ echo $r['email']; } 

I appreciate any help with this,

Thanks in advance!

+7
eloquent laravel
source share
1 answer

You need to use it as an object:

 $result = DB::select($query); foreach($result as $r){ echo $r->email; } 

Or if for some reason you want to use it as an array, you need to convert it first:

 $result = DB::select($query)->toArray(); foreach($result as $r){ echo $r['email']; } 
+12
source share

All Articles