【问题标题】:how to acces nested objects in blade templates如何访问刀片模板中的嵌套对象
【发布时间】:2015-03-18 16:57:51
【问题描述】:

让我告诉你我有什么。

//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语句执行正确

string 'select * from albums where albums.id = '{"id":19,"title":"Post no 19","re​​ad_more":"Lorem i'... (长度=786) 字符串'从albums中选择*,其中albums.post_id ='19''(长度=54) string 'select * from images where images.album_id in ('4')' (length=57)

我想访问刀片中的照片对象,所有内容都解析为模板,但是当我尝试获取照片时

@foreach($albums->照片为 $photo)

未定义属性:Illuminate\Database\Eloquent\Collection::$Photos(查看:

//发布模型

public function album()
    {
        return $this->hasOne('Album');

    }

//专辑型号

public function Photos(){

        return $this->hasMany('Image');
}

【问题讨论】:

    标签: laravel


    【解决方案1】:

    试试

    @foreach($albums->Photos()->get() as $photo)
    {{$photo->attribute}}
    @endforeach
    

    你需要调用持有关系的函数然后使用

    get()

    方法返回一个 Eloquent 集合,然后使用 foreach 循环对其进行迭代。

    【讨论】:

    • 没问题,您也可以尝试创建访问器以获取更多信息 go here
    【解决方案2】:
    @foreach($albums as $album)
        @foreach($album->Photos as $photo)
        <p>{{ $photo->image }}</p>
        @endforeach
        @endforeach
    

    这就是答案

    【讨论】:

      猜你喜欢
      • 2017-12-09
      • 1970-01-01
      • 1970-01-01
      • 2015-12-01
      • 2012-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-08
      相关资源
      最近更新 更多