Im在我的刀片文件中显示@foreach
语句中的数据库结果时遇到问题。我根本没有得到任何错误。Laravel 5:@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);
}
我的DB型号:
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()
我可以看到字符串“没有文章”,但我的foreach没有显示任何内容。
做一个DD($文章)
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: []
}
你可以做DD内($ articles)在索引方法内? – Digitlimit