区别于 Laravel 5 无法正常工作

Distinct in Laravel 5 not working properly

我想要来自我的数据库的不同名称 table.The table 包含列 ID 和名称。

我试过了

$diff=Crud::distinct('name')->get();
$diff=Crud::distinct()->get();

他们两个都不是 worked.They 返回的结果是 duplicates.Here Crud 是我的 eloquent 模型。

但是当我使用

$cnt=Crud::distinct()->count('name'); //returns the correct count

$u=Crud::all();
$diff=$u->unique('name'); //returns the distinct names

为什么我在使用 distinct 时会返回重复的值?我想通过使用不同的名称来区分。

谢谢

distinct() 不接受参数。使用以下选项之一:

$diff = Crud::distinct()->pluck('name');

$diff = Crud::distinct()->get(['name']);

$diff = Crud::distinct()->select('name')->get();