让我告诉你我的所作所为。
//Post Controller
public function showPost(Post $post)
{
$albums = $post->album()->with('photos')->get();
$comments = $post->comments()->where('approved', '=', 1)->get();
$this->layout->title = $post->title;
$this->layout->main = View::make('home')->nest('content','posts.single', compact('post', 'comments','albums'));
}
mysql语句正确执行
字符串'select * from albums
其中albums
。id
='{“id”:19,“title”:“post no 19”,“read_more”:“Lorem我......(长度= 786)
字符串'select * from albums
其中albums
。post_id
='19''(长度= 54)
字符串'select * from images
images
。album_id
in('4')'(length = 57)
我想访问刀片中的Photo对象,一切都解析到模板但是当我尝试获取照片时
@foreach($ albums->照片为$ photo)
未定义属性:Illuminate \ Database \ Eloquent \ Collection :: $ Photos(查看:
//发布模型
public function album()
{
return $this->hasOne('Album');
}
//专辑模型
public function Photos(){
return $this->hasMany('Image');
}
答案 0 :(得分:0)
尝试
@foreach($albums->Photos()->get() as $photo)
{{$photo->attribute}}
@endforeach
您需要调用保存关系的函数,然后使用
的get()
返回Eloquent Collection然后使用foreach循环迭代它的方法。
答案 1 :(得分:0)
@foreach($albums as $album)
@foreach($album->Photos as $photo)
<p>{{ $photo->image }}</p>
@endforeach
@endforeach
得出答案