我在刀片文件的@foreach
语句中显示数据库的结果时遇到问题。我一点也不犯错误。
控制器文件:
public function index()
{
/**
* Retrieve all articles
* where status is published [1]
* and order them by published date decending
*
* @var articles
*/
$articles = Article::where( function($query) {
return $query
->where('eHArt_Status', '1')
->orderBy('published_at', 'desc')
->paginate(10);
});
// return articles feed
return view('build.4-.feeds.articles')->with('articles', $articles);
}
我的数据库模型:
class Article extends Authenticatable
{
/**
* The database table used by the model
*
* @var string
*/
protected $table = 'eHArticle';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
// fillable article fields
'eHArt_OldArtID',
'eHArt_Title',
'eHArt_Content',
'eHArt_Author',
'eHArt_Status',
'eHArt_Img',
'published_at'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
// no hidden fields
];
}
我的视图文件:
@section('feed')
<!-- check if there is articles -->
@if ( !$articles->count() )
<p>No articles</p>
@else
@foreach ( $articles as $article )
<p>Article</p>
@endforeach
@endif
@endsection
如果我从$articles->count()
中去掉"!",我可以看到字符串"No articles",但我的foreach没有显示任何内容。
做dd($articles)
Builder {#159 ▼
#query: Builder {#158 ▼
#connection: MySqlConnection {#154 ▶}
#grammar: MySqlGrammar {#155 ▶}
#processor: MySqlProcessor {#156}
#bindings: array:6 [▶]
+aggregate: null
+columns: null
+distinct: false
+from: "eHArticle"
+joins: null
+wheres: array:1 [▶]
+groups: null
+havings: null
+orders: null
+limit: null
+offset: null
+unions: null
+unionLimit: null
+unionOffset: null
+unionOrders: null
+lock: null
#backups: []
#bindingBackups: []
#operators: array:26 [▶]
#useWritePdo: false
}
#model: Article {#152 ▼
#table: "eHArticle"
#fillable: array:7 [▶]
#hidden: []
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: []
#original: []
#relations: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: false
+wasRecentlyCreated: false
}
#eagerLoad: []
#macros: []
#onDelete: null
#passthru: array:11 [▶]
#scopes: []
}
如果使用Builder,请在查询后使用get()方法。
$articles = Article::where( function($query) {
return $query
->where('eHArt_Status', '1')
->orderBy('published_at', 'desc')
->paginate(10);
})->get();
否则您应该将查询更改为
$articles = Article::where('eHArt_Status', '1')->orderBy('published_at', 'desc')->paginate(10);
看看这个https://laravel.com/docs/5.1/pagination#paginating-雄辩的结果和https://laravel.com/docs/5.1/pagination#displaying-视图中的结果
您需要在<p>
标签{{$article}}
中回显它
@section('feed')
@if(count($articles))
@foreach ( $articles as $article )
<h1>{{$article->eHArt_Title}}</h1>
<p>{{$article->eHArt_Content}}</p>
@endforeach
@else
<p>No articles</p>
@endif
@endsection
{!! $articles->render() !!}
使用if(count($articles) > 0)
:
@section('feed')
<!-- check if there is articles -->
@if(count($articles) > 0)
@foreach ($articles as $article)
<p>Article</p>
@endforeach
@else
<p>No articles</p>
@endif
@endsection
您可以在文档中阅读有关count()
方法的更多信息。
此外,您应该使用此查询来获得正确的结果:
Article::where('eHArt_Status', '1')->orderBy('published_at', 'desc')->paginate(10);