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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40574019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:44:12  来源:igfitidea点击:

Undefined property: Illuminate\Pagination\LengthAwarePaginator::$id (View: F:\project\resources\views\admin\carousels\index.blade.php)

phplaravellaravel-5.3laravel-blade

提问by Zachary Dale

when I try to pass variable data to view I get this error, I can't find any document about this problem My controller(CarouselController.php)

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

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

My view(index.blade.php)

我的观点(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>

采纳答案by Saumya Rastogi

You foreach loop contains the code:

你 foreach 循环包含代码:

$carousels->id;

which seems to be getting a single object from collection, which isn't the right way, you should try this:

这似乎是从集合中获取单个对象,这不是正确的方法,您应该尝试以下操作:

@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

Hope this helps!

希望这可以帮助!