如何修复 Laravel 视图中未定义的变量 $data 错误?

huangapple go评论57阅读模式
英文:

How to Fix Undefined Variable $data Error in Laravel Views?

问题

Undefined variable $data:
如何修复 Laravel 视图中未定义的变量 $data 错误?

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?

英文:

Undefined variable $data:
如何修复 Laravel 视图中未定义的变量 $data 错误?

But I have declared $data on the controller like this:

public function coba()
{
    $data = coba::all();

    return view(&#39;coba&#39;);
}

And the view like this

coba.blade.php

@foreach ($data as $w)
    &lt;tr&gt;
        &lt;td&gt;{{$w-&gt;name}} &lt;/td&gt;
        &lt;td&gt;{{$w-&gt;no}}&lt;/td&gt;
        &lt;td&gt;{{$w-&gt;fraksi}}&lt;/td&gt;
        &lt;td&gt;{{$w-&gt;dapil}}&lt;/td&gt;
    &lt;/tr&gt;
@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(&#39;coba&#39;); 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(&#39;coba&#39;, [&#39;data&#39; =&gt; $data]);
}

huangapple
  • 本文由 发表于 2023年5月24日 23:03:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76324947.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定