mercredi 5 octobre 2016

Undefined index 0 php laravel (Might be duplicate)

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)

Don't know what's wrong am i doing, struggling and googling, didn't found a solution.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire