Search code examples
jsoneloquentlaravel-5.1laravel-bladelaravel-collection

Laravel 5.1 Eloquent collection not returning correct results


I have an eloquent collection {{ $questions }}, when i output it inside a blade template i get the following results:

[{"question_num":0,"survey_id":2,"question_text":"test","expected_answer":1},
  {"question_num":1,"survey_id":2,"question_text":"test","expected_answer":1}] 

As you can see there are exactly two objects. Now when I apply this filter {{ $questions->where('question_num','=', 0) }}, I get the following results which is correct:

[{"question_num":0,"survey_id":2,"question_text":"test","expected_answer":1}] 

But when I apply the following filter {{ $questions->where('question_num','=', 1) }}, I get an empty result, why is that, when clearly the collection has a question_num with value of 1?

[]

I've been scratching my head all day with this!


Solution

  • The problem here is that you use operator, here but Collection signature for where method is:

    where( string $key, mixed $value, bool $strict = true)
    

    so in both cases, you should use:

    {{ $questions->where('question_num', 0) }}
    

    and

    {{ $questions->where('question_num', 1) }}
    

    to get result you expect