从 table Laravel 获取唯一值

Get unique value from table Laravel

我想获取所有具有一个唯一列的行。我的代码的问题是它仅在我 select 只有那一列时有效,但最后我需要有多个列。

$values= Model::select('id','value_first','gender_first','value_second','gender_second')->groupBy('value_first')->get();

根据 Laravel 文档,您可以使用 distinct 相同的方法

The distinct method allows you to force the query to return distinct results for e.g

$values= Model::select('id','value_first','gender_first','value_second','gender_second')->distinct('value_first')->get();

参考:Laravel-> Database: Query Builder -> Selects

这是您的问题的解决方案

$values= Model::select('id','value_first','gender_first','value_second','gender_second')->distinct('value_first')->get();