1

在我的刀片模板中,我有以下 $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方法时它会返回null->get('question_text', null)

4

1 回答 1

1

请改用该first()方法。在 Laravel 中返get()​​回一个可数组的集合

于 2016-02-29T02:58:57.533 回答