我无法在我的刀片文件中的@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
如果我采取&#34;!&#34;远离$articles->count()
我可以看到字符串&#34;没有文章&#34;,但我的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: []
}
答案 0 :(得分:1)
如果使用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-eloquent-results和https://laravel.com/docs/5.1/pagination#displaying-results-in-a-view
您需要在<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() !!}
答案 1 :(得分:0)
使用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
您可以在the documentation中详细了解count()
方法。
此外,您应该使用此查询来获得正确的结果:
Article::where('eHArt_Status', '1')->orderBy('published_at', 'desc')->paginate(10);