code-8
code-8

Reputation: 58642

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'];

Upvotes: 0

Views: 1090

Answers (2)

N69S
N69S

Reputation: 17206

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();
}

Upvotes: 5

Kaz
Kaz

Reputation: 1258

Try orderBy() instead of groupBy().

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

Upvotes: 1

Related Questions