0

MySQL structure

id
key
display_name
value
details
group
created_at
updated_at

Laravel resource index

    public function index()
    {

        $settings = Setting::groupBy('group')->select('id','group as name')->orderBy('id', 'ASC')->get();
        
        return view('settings.general'), compact('settings'));
    }

When I run the code its how error :

SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'demodb.settings.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select id, group as name from settings group by group order by id asc)

2 Answers2

1

Every column has to be in the GROUP By or have a aggregation function like in the example MIN(id)

public function index()
{

    $settings = Setting::groupBy('group')->select('MIN(d) AS id,group as name')->orderBy('id', 'ASC')->get();
    
    return view('settings.general'), compact('settings'));
}
nbk
  • 45,398
  • 8
  • 30
  • 47
0

When your group by 'group' you can have more than one id per group so it's a pb of logic, you cannot* have id in the select list.

*since your query cannot guess

2 solutions depending of what you want to do :

  • retrieve id from the select

  • add id to the group by

Dri372
  • 1,275
  • 3
  • 13