How to get distinct values for non-key column fields in Laravel?

前端 未结 14 1666
太阳男子
太阳男子 2020-12-01 09:09

This might be quite easy but have no idea how to.

I have a table that can have repeated values for a particular non-key column field. How do I write a SQL query usin

14条回答
  •  清歌不尽
    2020-12-01 09:11

    in eloquent you can use this

    $users = User::select('name')->groupBy('name')->get()->toArray() ;
    

    groupBy is actually fetching the distinct values, in fact the groupBy will categorize the same values, so that we can use aggregate functions on them. but in this scenario we have no aggregate functions, we are just selecting the value which will cause the result to have distinct values

提交回复
热议问题