我遇到了未定义变量的问题:图像 - 错误消息。请检查以下代码。请支持我。
'Route::get('partials/recent-gallery','ImageGalleryController@recentview');'
'public function recentview()
{
$images = ImageGallery::all();
return view('partials.recent-gallery', ['images' => $images]);
}'
---首页 '@include('partials/recent-gallery')'
---- 查看页面-----recent-gallery.blade.php------
'@if($images->count())
@foreach($images as $image)
{{ $image->galley_image }}
@endforeach
@endif'
-------------------错误--------------
$images 未定义
将数据传递给子视图的步骤是在刀片语法的帮助下将数据从父视图(在您的情况下是主页)传递到子视图(最近的图库)。
您必须首先将数据从您的函数传递到主页,该函数初始化您的主页路由并为您的子视图传递图像数据。
public function viewHomePage()
{
$images = ImageGallery::all();
// passing your image data to the homepage not to your subview
return view('home', ['images' => $images]);
}
您不必为子视图设置单独的路由,因为它是一个子视图。通过刀片语法 将您从主页视图函数传递的数据传递到子视图@include('partials/recent-gallery', ['images' => $images])
。请注意,我们没有路由子视图web.php
或具有控制器功能来传递image
数据。
主页.blade.php
<!-- homepage content -->
@include('partials/recent-gallery', ['images' => $images])
然后您可以使用参数访问子视图中的图像数据$images
。您可以使用dd()
方法检查传递的数据并传递参数,dd($images)
以便进行调试。
partials/recent-gallery.blade.php
@if($images->count())
@foreach($images as $image)
{{ $image->galley_image }}
@endforeach
@endif
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句