在Laravel 5中无法正常工作 [英] Distinct in Laravel 5 not working properly

查看:102
本文介绍了在Laravel 5中无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要数据库表中的不同名称。该表包含列ID和名称。



我尝试过


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

$ diff = Crud ::




这两个都不起作用。它们返回重复的结果。在这里,Crud是我雄辩的模型。



但是当我使用

  $ cnt = Crud :: distinct() -> count('name'); //返回正确的计数

  $ u = Crud :: all(); 
$ diff = $ u->独特的(名称); //返回唯一名称

为什么在我使用唯一名称时返回重复的值?我想要使​​用distinct。


谢谢

解决方案

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

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

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

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


I want the distinct names from my database table.The table contains column id and name.

I have tried

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

Both of them not worked.They are returning the result with duplicates.Here Crud is my eloquent model.

But when i use

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

and

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

Why duplicate values are returned while i'm using the distinct? I want distinct names by using distinct.

Thanks.

解决方案

distinct() doesn't accept arguments. Use one of these options:

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

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

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

这篇关于在Laravel 5中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆