本文介绍了Laravel-视图上未定义的变量$ data的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对此有疑问.
但是我已经像这样在控制器上声明了$ acara
But I have declared $acara on the controller like this
控制器
use App\events;
public function lihat_acara()
{
$data['acara'] = events::all();
return view('admin.home')->with($data);
}
和这样的视图
home.blade.php
@foreach($acara as $key)
<tbody>
<tr>
<td>{{ $key->nama_acara }}</td>
<td>{{ $key->kategori_acara }}</td>
<td>{{ $key->lokasi_acara }}</td>
</tr>
</tbody>
@endforeach
我的代码有什么问题?任何想法?:)
What's wrong with my code? Any idea? :)
推荐答案
use App\events;
public function lihat_acara()
{
$data['acara'] = events::all();
return view('admin.home', ['acara' => $data['acara'] ]);
}
OR
use App\events;
public function lihat_acara()
{
$data['acara'] = events::all();
return view('admin.home')->with('acara', $data['acara']);
}
OR
use App\events;
public function lihat_acara()
{
$data['acara'] = events::all();
return view('admin.home')->withAcara($data['acara']);
}
从laravel官方网站将数据传递给视图,您可以找到所有方法从控制器发送数据以查看.
From this laravel official site Passing data to view, you can find all the way to send data from controller to view.
这篇关于Laravel-视图上未定义的变量$ data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!