[英]Laravel 5.1 querying a collection inside blade template
在我的刀片模板中,我有以下$ questions集合
Collection {
#items: array:2 [
0 => Question {
....
#attributes: array:7 [
"survey_id" => 3
"question_num" => 0
"question_text" => "test"
"expected_answer" => 1
"created_at" => "0000-00-00 00:00:00"
"updated_at" => "0000-00-00 00:00:00"
]
}
1 => Question {#318 ▶}
]
}
要检查question_num是否存在,我可以执行以下操作:
@foreach ($questions as $question) {
@if ($question->question_num == 0)
{{ $question->question_text }}
@endif
@endforeach
但是,有什么方法可以执行这样的操作,以便无需使用循环就可以直接查询集合?
{{ $questions->where('question_num','=', 0)->get('question_text', null) }}
应用where方法$questions->where('question_num','=', 0)
给我以下结果:
[{"survey_id":3,"question_num":0,"question_text":"test","expected_answer":1,"created_at":"2016-02-28 14:20:17","updated_at":"2016-02-28 14:20:17"}]
那么为什么当我链接get方法->get('question_text', null)
时,为什么它会返回null?
请改用first()
方法。 在Laravel中get()
返回一个可数组的集合
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.