0

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);
    }
Davit Zeynalyan
  • 8,418
  • 5
  • 30
  • 55
Hesham
  • 35
  • 6

3 Answers3

1

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

Pavel Lint
  • 3,252
  • 1
  • 18
  • 18
0

Check if the following code gives you the "There are no infos". If it does, that means that the $infos variable in the controller is not returning data, and thus giving you the error

@if(empty($infos))
    <tr><td>There are no infos</td></tr>
@else
   @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
@endif
Davit Zeynalyan
  • 8,418
  • 5
  • 30
  • 55
mustafaj
  • 305
  • 1
  • 12
0

You can use forelse will be better:

@forelse($infos as $key => $info)
    <tr>
        <td>{{ $info->id }}</td>
        <td>{{ $info->name}}</td>
        <td>{{ $info->code }}</td>
        <td>{{ $info->phone }}</td>
        <td>{{ $info->phone2 }}</td>
    </tr>
@empty
    <tr>
        <td>Data Not Found</td>
    </tr>
@endforelse

Controller:

public function index()
{
    $info = Info::latest()->get();
    return view('info.admin')->with(['infos'=> $info]);
}
Nitin Sharma
  • 419
  • 3
  • 12