2016-02-27 71 views
1

在我的刀模板,我有以下$问题收集Laravel 5.1查询刀片模板中的集合

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) }} 

运用其中,方法$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"}] 

那么,为什么它reuturn空当我链get方法->get('question_text', null)

+0

https://laravel.com/docs/5.1/collections#method-filter – KoIIIeY

+0

为什么没有get方法的工作? – adam78

回答

1

使用first()方法来代替。在Laravel get()返回arrayable收集