英文:
How to Fix Undefined Variable $data Error in Laravel Views?
问题
But I have declared $data on the controller like this:
public function coba()
{
    $data = coba::all();
    return view('coba');
}
And the view like this
coba.blade.php
@foreach ($data as $w)
    <tr>
        <td>{{$w->name}} </td>
        <td>{{$w->no}}</td>
        <td>{{$w->fraksi}}</td>
        <td>{{$w->dapil}}</td>
    </tr>
@endforeach
What's wrong?
英文:
But I have declared $data on the controller like this:
public function coba()
{
    $data = coba::all();
    return view('coba');
}
And the view like this
coba.blade.php
@foreach ($data as $w)
    <tr>
        <td>{{$w->name}} </td>
        <td>{{$w->no}}</td>
        <td>{{$w->fraksi}}</td>
        <td>{{$w->dapil}}</td>
    </tr>
@endforeach
What's wrong?
答案1
得分: 1
你遇到的问题是因为在控制器的return view('coba');语句中缺少变量$data。
为了解决这个问题,你需要将变量$data传递给视图,以便在Blade模板中访问它。
public function coba()
{
    $data = coba::all();
    return view('coba', ['data' => $data]);
}
英文:
The issue you're encountering is due to the missing variable $data in the return view('coba'); statement of your controller.
To fix this, you need to pass the $data variable to the view so that it is accessible within the blade template.
public function coba()
{
    $data = coba::all();
    return view('coba', ['data' => $data]);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论