未定义属性:Illuminate \ Pagination \ LengthAwarePaginator :: $ id(查看:F:\ project \ resources \ views \ admin \ carousels \ index.blade.php)

时间:2016-11-13 12:50:14

标签: php laravel laravel-5.3 laravel-blade

当我尝试传递可变数据进行查看时出现此错误,我无法找到有关此问题的任何文档 我的控制器(CarouselController.php)

    public function index()
{
$carousels = Carousel::orderBy('created_at', 'asc')->paginate(12);
return view('admin.carousels.index')->withCarousels($carousels);
}

我的观点(index.blade.php)

<div class="row">
    <div class="col-md-9">
        <h1>All Images</h1>
    </div>
    <div class="col-md-3">
        <a href="{{ route('carousels.create') }}" class="btn btn-lg btn-block btn-primary  ">Create New carousel</a>
    </div>
    <div class="col-md-12">
        <hr>
    </div>
</div>{{-- end of the row --}}
<div class="row">
    <div class="col-md-12">
            <div class="row">
     @foreach($carousels as $photo)
        <div class="col-xs-6 col-md-3">
        {!!  Form::open( array('route'=>array('carousels.destroy', $carousels->id),'method'=>'DELETE')) !!}

        {!! Form::submit('Delete', array('class'=>"btn btn-danger btn-sm tours-delete tour-index-delete"))!!}

        {!! Form::close() !!}
        <a href="{{ url($photo->path) }}" class="thumbnail" data-lity>
        <img class="img-responsive" src="{{ $photo->path }}" alt="">
        </a>
        </div>
     @endforeach

</div>
    </div>
    <div class="text-center">
            {!! $carousels->links(); !!}
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

你foreach循环包含代码:

$carousels->id;

似乎是从集合中获取单个对象,这不是正确的方法,你应该试试这个:

@foreach($carousels as $carousel)
  <div class="col-xs-6 col-md-3">
  {!!  Form::open( array('route'=>array('carousels.destroy', $carousel->id),'method'=>'DELETE')) !!}

  {!! Form::submit('Delete', array('class'=>"btn btn-danger btn-sm tours-delete tour-index-delete"))!!}

  {!! Form::close() !!}
  <a href="{{ url($carousel->path) }}" class="thumbnail" data-lity>
  <img class="img-responsive" src="{{ $carousel->path }}" alt="">
  </a>
  </div>
@endforeach

希望这有帮助!