无法使用 @foreach 遍历 Blade 中的 Eloquent 对象
Cannot loop through Eloquent object in Blade using @foreach
我看过类似的 question 但不幸的是答案不适用于我。
我知道我的对象确实包含我的数据,但在我看来从未调用过@foreach 的内部。
这是我的控制器:
$newest_article = $this->article->orderBy('created_at', 'DESC')->first();
$articles = Article::allButFirst($newest_article->id)->orderBy('created_at', 'DESC');
return View::make('site/news/index', compact('newest_article', 'articles'));
我模型的 scopeAllButFirst 函数:
public function scopeAllButFirst($query, $id){
return $query->where('id', '<>', $id);
}
最后是我的观点:
@extends('site.layouts.default')
@section('content')
<section id="news-main">
<div class="container">
<!-- all posts wrapper -->
<div class="news-box-wrap js-packery clearfix" data-packery-options='{ "itemSelector": ".news-item"}'>
@foreach($articles as $article)
<div class="col-sm-4 news-item">
<a href="{{{ $article->url() }}}" class="news-box-inner">
<div class="news-box-image-wrap">
@if($article->image->url('tile_landscape'))
<img src="{{{ $article->image->url('tile_landscape') }}}" class="news-box-image">
@endif
<span class="news-box-icon">
<i class="fa fa-align-left"></i>
</span>
</div>
<div class="news-box-bottom">
<div class="news-box-data">
<span class="news-box-cat">News</span>
•
<span class="news-box-time">{{{ $article->date() }}}</span>
</div>
<h3>{{{ $article->title }}}</h3>
<div class="news-box-det">
<span class="news-box-author">{{{ $article->author->username }}}</span>
<span class="news-box-like"><i class="fa fa-smile-o fa-lg"></i> 225</span>
<span class="news-box-comment"><i class="fa fa-comment-o fa-lg"></i> 16</span>
</div>
</div>
</a>
</div>
@endforeach
</div>
@stop
我需要遍历 Eloquent 对象,因为我必须访问模型中的某些方法 class。
仅供参考,我关注andrewelkins/Laravel-4-Bootstrap-Starter-Site
更新
在我的调试中我注意到我是否替换了
$articles = Article::allButFirst($newest_article->id)->orderBy('created_at', 'DESC');
和
$articles = Article::all();
我没有这个问题。
对于那些面临问题的人,答案一如既往地在 API 中。事实证明,您必须调用 get() 方法才能接收集合。这就是 all() returns。所以简单地改变
$articles = Article::allButFirst($newest_article->id)->orderBy('created_at', 'DESC');
至
$articles = Article::allButFirst($newest_article->id)->orderBy('created_at', 'DESC')->get();
解决问题。
希望对您有所帮助
我看过类似的 question 但不幸的是答案不适用于我。
我知道我的对象确实包含我的数据,但在我看来从未调用过@foreach 的内部。
这是我的控制器:
$newest_article = $this->article->orderBy('created_at', 'DESC')->first();
$articles = Article::allButFirst($newest_article->id)->orderBy('created_at', 'DESC');
return View::make('site/news/index', compact('newest_article', 'articles'));
我模型的 scopeAllButFirst 函数:
public function scopeAllButFirst($query, $id){
return $query->where('id', '<>', $id);
}
最后是我的观点:
@extends('site.layouts.default')
@section('content')
<section id="news-main">
<div class="container">
<!-- all posts wrapper -->
<div class="news-box-wrap js-packery clearfix" data-packery-options='{ "itemSelector": ".news-item"}'>
@foreach($articles as $article)
<div class="col-sm-4 news-item">
<a href="{{{ $article->url() }}}" class="news-box-inner">
<div class="news-box-image-wrap">
@if($article->image->url('tile_landscape'))
<img src="{{{ $article->image->url('tile_landscape') }}}" class="news-box-image">
@endif
<span class="news-box-icon">
<i class="fa fa-align-left"></i>
</span>
</div>
<div class="news-box-bottom">
<div class="news-box-data">
<span class="news-box-cat">News</span>
•
<span class="news-box-time">{{{ $article->date() }}}</span>
</div>
<h3>{{{ $article->title }}}</h3>
<div class="news-box-det">
<span class="news-box-author">{{{ $article->author->username }}}</span>
<span class="news-box-like"><i class="fa fa-smile-o fa-lg"></i> 225</span>
<span class="news-box-comment"><i class="fa fa-comment-o fa-lg"></i> 16</span>
</div>
</div>
</a>
</div>
@endforeach
</div>
@stop
我需要遍历 Eloquent 对象,因为我必须访问模型中的某些方法 class。
仅供参考,我关注andrewelkins/Laravel-4-Bootstrap-Starter-Site
更新
在我的调试中我注意到我是否替换了
$articles = Article::allButFirst($newest_article->id)->orderBy('created_at', 'DESC');
和
$articles = Article::all();
我没有这个问题。
对于那些面临问题的人,答案一如既往地在 API 中。事实证明,您必须调用 get() 方法才能接收集合。这就是 all() returns。所以简单地改变
$articles = Article::allButFirst($newest_article->id)->orderBy('created_at', 'DESC');
至
$articles = Article::allButFirst($newest_article->id)->orderBy('created_at', 'DESC')->get();
解决问题。
希望对您有所帮助