Reputation: 70
I got an error in SQL when trying to join and group the id using Laravel.
Column 'push_notifications.title' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause
I've been trying to use Max and Min but I still got an error.
Here's my code :
$data = $this->model->select( 'push_notifications.id', 'push_notifications.title', 'push_notifications.description', 'push_notifications.attachment', 'push_notifications.feature_id', 'push_notifications.target_id', 'features.title as feature', 'push_notifications.end_at')
->join('push_notification_users', 'push_notification_users.push_notification_id', '=', 'push_notifications.id')
->join('push_notification_roles', 'push_notification_roles.push_notification_id', '=', 'push_notifications.id')
->leftJoin('features', 'features.id', '=', 'push_notifications.feature_id')
->where( 'push_notifications.notif_target', 'homepage' )
->where(function($role) {
$roles = RoleUser::where('user_id',$this->user->id)->first();
$role->where( 'push_notification_roles.deleted_at', null )
->where( 'push_notification_roles.role_id', $roles->role_id );
})
->orWhere(function($user) {
$user->where( 'push_notification_users.deleted_at', null )
->where( 'push_notification_users.user_id', $this->user->id );
})
->where( 'push_notifications.end_at', '>', Carbon::now()->toDateTimeString() )
->groupBy( 'push_notifications.id' )
->get();
If you have any solution just tell Me, I will really appreciate. Thank You
Upvotes: 0
Views: 215
Reputation: 81
'push_notifications.title' is not found. May there be some spelling mistake. Check thoroughly. If not so then please attach screenshot of your table structure i.e. push_notifications.
Also another reason is that there must be diff-diff values of push_notifications.title for 'push_notifications.id' and there can be multiple 'push_notifications.title' for single 'push_notifications.id' in groupBy. Only select 'push_notifications.id' and some other unique columns will work in your case.
refer this: https://www.techonthenet.com/sql/group_by.php
Upvotes: 1