I’m kinda new to programming in general. I’m trying to loop through a table. There is a connection to the DB already. It’s giving me – "Object of class IlluminateDatabaseMySqlConnection could not be converted to string"
class ProductController extends Controller
{
function list(){
$serve = DB::table('sport');
foreach($serve as $val){
return strval($val);
}
}
}
That’s my code so far. How can I fix that issue?
Thanks!
5
Answers
This is how it looks now and it returns the values in the localhost.
for iterating throgh table rows , you need to make your objects iterable ,
when you do :
you are not actually make it iterable ;
use get() method to make it terable:
Now you can iterate over it .
notice that each $serve item is a model of Sport model
and now you can access the fields of sport table from this method
for eample if you have the fields in you sports table :
you can access them like this :
finally please try to give better naming
and change $serves to $sports
and $serve to $sport to enhance code readibility.
You have to get the sport table data first and then loop over on it:
To loop in a table you can use: