在laravel 5.4中加入时“此集合实例上不存在属性”错误

时间:2018-07-16 17:20:35

标签: laravel laravel-5.4

UserAcademyController.php

    public function view_video()
{
    $materialList = DB::table('material')
        ->join('material_group', 'material.group_id', '=', 'material_group.id')
        ->select('material.*', 'material_group.group_thumbnail_url')
        ->get();

    return view('user-academy.view_video', ['materialList' => $materialList]);
}

view_video.blade.php

 @foreach ($materialList as $material)
        <div class="col-sm-6 col-lg-4 col-md-4">
            <div class="thumbnail">
                <a href="{!! url('user-academy/view/' . $material->id)!!}">
                    @if($material->thumbnail_url !== null)
                        <img src="{{$material->thumbnail_url}}"
                             style="width: 700px; height: 300px">
                    @elseif($materialList->group_thumbnail_url !== null)
                        <img src="{{$materialList->group_thumbnail_url}}"
                             style="width: 700px; height: 300px">
                    @elseif($material->thumbnail_url !== null && $materialList->group_thumbnail_url !== null)
                        <img src="http://www.independentmediators.co.uk/wp-content/uploads/2016/02/placeholder-image.jpg"
                             style="width: 700px; height: 300px">
                    @endif
                </a>
                <div class="caption">
                    <h4 class="pull-right text-success">${{$material->price}} </h4>

                    <h4>
                        <a href="{!! url('user-academy/view/' . $material->id)   !!}">{{ $material->title }}</a>
                    </h4>
                    <hr>
                    <p class="description">{!!  $material->description  !!}  </p>
                </div>
            </div>
        </div>
    @endforeach

This is the "material" table

This is the "material_group" table

即使我完全复制了laravel文档中的所有内容,并遵循了许多有关该主题的视频,但还是遇到了这个错误,我是一名新程序员,所以这可能是一个非常明显的错误,但是我花了很多时间在看代码和只是找不到错误在哪里!

1 个答案:

答案 0 :(得分:1)

您正在尝试访问group_thumbnail_url类的Collection属性,该属性肯定不存在,这就是您收到该错误的原因。

解决方案是将$materialList->group_thumbnail_url替换为material->group_thumbnail_url