【发布时间】:2017-06-14 07:02:53
【问题描述】:
我在表格中设置了两个模型。并制作了一个表格来填写两个表格,并且效果很好
Tour.php
public function featuredImage()
{
return $this->hasOne('App\FeaturedImage');
}
tours表
id|name|content|featured_status
featuredImage.php
public function tour()
{
return $this->belongsTo('App\Tour');
}
Featured_images 表
id|tour_id|path|name
我的控制器中用于将数据传递给视图的代码。
$tours = Tour::where('featured', 1)->get();
return view('public.pages.index')
->withTours($tours);
我认为的代码
@foreach($tours as $featured)
<div class="thumbnail">
<img src="{{$featured->featuredimage->path}}" alt="{{$featured->featuredImage->name}}">
</div>
<h4>{{$featured-name}}</h4>
@endforeach
问题是我无法通过写作来获取特色图片
{{$featured->featuredimage->path}}
错误是
Trying to get property of non-object
在线{{$featured->featuredimage->path}}。我在之前的项目中使用过这种方法,效果很好,但在这个项目中并不顺利。
我尝试用{{$featured->featuredImage->path}} 替换{{$featured->featuredimage->path}},但没有成功。
【问题讨论】:
-
什么是
withTours?
标签: php laravel laravel-5.3 laravel-blade