This might be a duplicate, but i couldn't found any solution to my problem. I am retrieving few records from database and trying to display them in view. But always getting Undefined index: 0 Exception. Here's controller code:
public function gettopfirms(){
$top = Top::all();
foreach ($top as $tFirm) {
$topFirms[] = Firm::whereId($tFirm['id'])->get()->toArray();
}
return view('top', compact('topFirms'));
}
And here's the view (top.blade.php) code:
<?php
foreach ($topFirms as $firm) {
echo "<pre>";
print_r($firm[0]); //or $firm[0]['name']
echo "</pre>";
}
?>
And here's the Error:
Undefined offset: 0 (View: xxxxx/resources/views/top.blade.php)
Here is when i simply print array in view
Array
(
[0] => Array
(
[id] => 7092
[rank] => 147
[name] => Grupo Grana y Montero (GyM),
)
)
Array
(
[0] => Array
(
[id] => 2 //And So On
Don't know what's wrong am i doing, struggling and googling, didn't found a solution.