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);
    }
2

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.