Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating through result set in Laravel controller

Tags:

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

Cannot use object of type stdClass as array 

Controller snippet:

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

I appreciate any help with this,

Thanks in advance!

like image 215
AnchovyLegend Avatar asked Jan 31 '15 20:01

AnchovyLegend


1 Answers

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 array, you need to convert it first:

$result = DB::select($query)->toArray();  foreach($result as $r){     echo $r['email']; } 
like image 90
Giedrius Avatar answered Oct 03 '22 10:10

Giedrius