I'm using Laravel Eloquent to pick out unique answers.
So in my Database I have 3 answers, But they are under two different questions.
So all I would need returning back is the number 2, Not the number 3.
My code so far is :
$foundation_scores = Foundation::where('user_id', $this->user_id)
->where('foundation_section', 'theory')
->get();
$foundation_scores->unique('foundation_question');
$foundation_scores = count($foundation_scores);
The unique('foundation_question') is returning 3. It should return 2
Any ideas why this would be?
My collection looks as follows (NOTE) I've outputted it as an array to avoide all the code Laravel Returns :
array:3 [
0 => array:12 [
"foundation_id" => 3
"user_id" => 17019
"foundation_section" => "theory"
"unit_id" => 0
"foundation_sub_section" => "Unit 1"
"foundation_question" => 0
"foundation_reference" => "Ref one way st"
"foundation_answer" => "Ref answer st"
"created_at" => "2017-10-25 11:57:05"
"updated_at" => "2017-10-25 11:57:05"
"deleted_at" => null
"friendly_date" => "25th October 2017"
]
1 => array:12 [
"foundation_id" => 4
"user_id" => 17019
"foundation_section" => "theory"
"unit_id" => 0
"foundation_sub_section" => "Unit 1"
"foundation_question" => 0
"foundation_reference" => "Red"
"foundation_answer" => "Ans"
"created_at" => "2017-10-25 12:02:01"
"updated_at" => "2017-10-25 12:02:01"
"deleted_at" => null
"friendly_date" => "25th October 2017"
]
2 => array:12 [
"foundation_id" => 5
"user_id" => 17019
"foundation_section" => "theory"
"unit_id" => 0
"foundation_sub_section" => "Unit 1"
"foundation_question" => 1
"foundation_reference" => "XY"
"foundation_answer" => "Z"
"created_at" => "2017-10-25 12:02:19"
"updated_at" => "2017-10-25 12:02:19"
"deleted_at" => null
"friendly_date" => "25th October 2017"
]
]
Is their a bug with unique. Or is it something I'm doing wrong?
$foundation_scores->unique('foundation_question');
is not assigning unique values to any new variable. Also unique()
does not change original collection.
So:
$unique_collection = $foundation_scores->unique('foundation_question');
echo count($unique_collection);