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

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

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:

  1. public function coba()
  2. {
  3. $data = coba::all();
  4. return view('coba');
  5. }

And the view like this

coba.blade.php

  1. @foreach ($data as $w)
  2. <tr>
  3. <td>{{$w->name}} </td>
  4. <td>{{$w->no}}</td>
  5. <td>{{$w->fraksi}}</td>
  6. <td>{{$w->dapil}}</td>
  7. </tr>
  8. @endforeach

What's wrong?

英文:

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

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

  1. public function coba()
  2. {
  3. $data = coba::all();
  4. return view(&#39;coba&#39;);
  5. }

And the view like this

coba.blade.php

  1. @foreach ($data as $w)
  2. &lt;tr&gt;
  3. &lt;td&gt;{{$w-&gt;name}} &lt;/td&gt;
  4. &lt;td&gt;{{$w-&gt;no}}&lt;/td&gt;
  5. &lt;td&gt;{{$w-&gt;fraksi}}&lt;/td&gt;
  6. &lt;td&gt;{{$w-&gt;dapil}}&lt;/td&gt;
  7. &lt;/tr&gt;
  8. @endforeach

What's wrong?

答案1

得分: 1

你遇到的问题是因为在控制器的return view('coba');语句中缺少变量$data

为了解决这个问题,你需要将变量$data传递给视图,以便在Blade模板中访问它。

  1. public function coba()
  2. {
  3. $data = coba::all();
  4. return view('coba', ['data' => $data]);
  5. }
英文:

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.

  1. public function coba()
  2. {
  3. $data = coba::all();
  4. return view(&#39;coba&#39;, [&#39;data&#39; =&gt; $data]);
  5. }

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:

确定