简体   繁体   中英

How to perform a groupBy in Laravel 5.8?

I have a table with 41 rows

I did

public function types()
{
    return DB::table('graphs')->groupBy('title')->get();
}

I kept getting

Can someone please help ?

I expected to get these 3

['MBN_PRIVATE','MBN_GUEST','SPWIFI'];

In MySql strict mode, you can't return non aggregated fields in a group by query. If you need only the title's values add a select or a pluck method

public function types()
{
    return DB::table('graphs')->groupBy('title')->pluck('title')->toArray();
}

Try orderBy() instead of groupBy().

public function types()
{
    return DB::table('graphs')->orderBy('title')->get();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM