Laravel Eloquent unique() - 似乎不起作用

Laravel Eloquent unique() - Seems to not work

我正在使用 Laravel Eloquent 来挑选独特的答案。

所以在我的数据库中我有 3 个答案,但它们属于两个不同的问题。

所以我只需要return返回的是数字 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')是returning 3。应该return2

知道为什么会这样吗?

我的 collection 如下所示(注意)我将其输出为数组以避免所有代码 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"
  ]
]

他们的错误是独一无二的。还是我做错了什么?

$foundation_scores->unique('foundation_question'); 没有将 unique 值分配给任何新变量。此外 unique() 不会更改原始集合。

所以:

$unique_collection = $foundation_scores->unique('foundation_question');
echo count($unique_collection);