Laravel 5.2中Eloquent或Query Builder中的唯一值的方法

时间:2017-05-10 07:37:13

标签: controller laravel-5.2 laravel-blade

这是方案

表:用户

id | name | grade ------ | ------|------ 1 | Mark | a 2 | Earl | a 3 | John | c 4 | Mike | d 5 | Matt | e

我想要做的是填写非重复或非重复成绩的成绩

所以它应该只显示

Ç
d
ë

我到目前为止

控制器
$grades = user::select('grade') ->distinct() ->get();

叶片
@foreach ($grades as $grade) {{ $grade }}<br> @endforeach<br><br>

但是,此查询提供了此

一个
ç
d
ë

1 个答案:

答案 0 :(得分:1)

尝试此查询: -

$grades = DB::select(DB::raw('SELECT grade , count(grade) as gradecount  FROM users  group 
by grade having gradecount = 1'));

                    OR

$grades = DB::table('users')
            ->select('*', DB::raw('COUNT(grade) as gradecount'))
            ->groupBy('grade')
            ->having('gradecount', '=',1)
            ->get();

希望它有所帮助!