【发布时间】:2016-06-11 02:46:21
【问题描述】:
在我的刀片模板中,我有以下 $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
【问题讨论】:
-
为什么get方法不起作用?
标签: php eloquent laravel-5.1 laravel-blade laravel-collection