Skip to content
Advertisement

Laravel : @foreach Undefined variable

i have error but in foreach :Undefined variable: infos that’s my View :

@foreach ($infos as $info)
    <tr>
        <td>{{ $info->id }}</td>
        <td>{{ $info->name}}</td>
        <td>{{ $info->code }}</td>
        <td>{{ $info->phone }}</td>
        <td>{{ $info->phone2 }}</td>
    </tr>
@endforeach

and my controller

    public function index()
    {
        $info = Info::latest()->get();
        return view('info.admin')->with('infos', $info);
    }

Advertisement

Answer

You should pass parameters to the view like this:

   return view('info.admin', ['infos' => $infos]);

What you’ve been doing before using with has a different effect, it flashes the data to the session. Check out this doc here

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement