2016-02-27 68 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收集