我正在使用Laravel Eloquent来挑选出独特的答案。
所以在我的数据库中我有3个答案,但它们有两个不同的问题。
所以我需要返回的是2号,而不是3号。
到目前为止我的代码是:
$foundation_scores = Foundation::where('user_id', $this->user_id)
->where('foundation_section', 'theory')
->get();
$foundation_scores->unique('foundation_question');
$foundation_scores = count($foundation_scores);
唯一('foundation_question')返回 3 。它应该返回 2
为什么会出现这种想法?
我的收藏如下(注)我已将其作为数组输出以避免所有代码Laravel返回:
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"
]
]
他们是一个独特的错误。或者是我做错了什么?
答案 0 :(得分:3)
$foundation_scores->unique('foundation_question');
未将唯一值分配给任何新变量。 unique()
也不会更改原始收藏。
所以:
$unique_collection = $foundation_scores->unique('foundation_question');
echo count($unique_collection);