How to use Group By with Join table in laravel [duplicate] - php

This question already has answers here:
MySQL : isn't in GROUP BY
(5 answers)
Closed 3 years ago.
When execute my query , I get error message like below.
$ads = DB::table('ads')
->join('adimages','adimages.ad_id','=','ads.id')
->select(
'adimages.ad_id',
'ads.ad_title',
'ads.created_at',
'ads.beds',
'ads.baths',
'ads.landmark',
'ads.city',
'ads.price',
'adimages.photo'
)
->groupBy('adimages.ad_id')
->get();
Here is the error I'm getting.
SQLSTATE[42000]: Syntax error or access violation: 1055 'nekton.ads.ad_title' isn't in GROUP BY (SQL: select adimages.ad_id, ads.ad_title, ads.created_at, ads.beds, ads.baths, ads.landmark, ads.city, ads.price, adimages.photo from ads inner join adimages on adimages.ad_id = ads.id group by adimages.ad_id)

In Laravel, you need to disable strict key in database.php file. Please follow below steps
1. Open config/database.php
2. Find strict key inside mysql connection settings
3. Set the value to false
I think this will help you.

Related

How to fix column name is not in a groupBy laravel?

I am facing this error for the past 2-3 days. I hold this task to check this later in my free time. But still, I am getting the same error. Please help me to find the error. I am tired to check all solutions on google. But nothing works.
Error
Illuminate\Database\QueryException: SQLSTATE[42000]: Syntax error or access violation: 1055 'dating-app.messages.id' isn't in GROUP BY (SQL: select * from messages group by room_id having is_read = 0) in file C:\xampp\htdocs\dating-app\vendor\laravel\framework\src\Illuminate\Database\Connection.php on line 692
Here is my query
$messages = Message::groupBy('room_id')->having('is_read', 0)->get();
return response()->json(['status' => true, 'message' => $messages]);`
Any solution appreciated!
That's because any column you use in a select statement must appear in the group by clause. So select * and group by don't get along. Remove groupBy('room_id') in your relationship and see how that goes.
Alternatively, disable MySQL strict mode by going into config/database.php and setting strict => false for MySQL, if you are using MySQL version >5.7.
source

Laravel DB:select() requires all columns in the group by

I need to run a custom query and I have tried all these methods
$sql = "SELECT acd.*, DATE_FORMAT(acd.cdate, '%d/%m/%Y') cdate_disp,
GROUP_CONCAT(CONCAT_WS(', ', car.type, car.session, crd.name) SEPARATOR '<br />') rd_names,
acb.booking_status my_booking_status
FROM app_data acd
INNER JOIN crmaccounts ca ON ca.id = acd.client_crm_id
LEFT JOIN crmaccount_rdids car ON car.account_id = ca.id
LEFT JOIN crmrd_ids crd ON crd.id = car.rd_id
LEFT JOIN app_bookings acb ON acb.call_ref_id = acd.call_ref AND acb.user_id = 12391
WHERE 1=1
AND acd.client_crm_id NOT IN (select account_id from bstaff WHERE user_id=12391)
GROUP BY acd.id
ORDER BY acd.cdate, ctiming";
DB:select($sql);
throws
Illuminate \ Database \ QueryException (42000)
SQLSTATE[42000]: Syntax error or access violation: 1055 'mydatabase.acd.call_ref' isn't in GROUP BY (SQL: ....)
DB::select($sql);
DB::raw($sql);
DB::select(DB::raw($sql));
//even with pdo
$sth = DB::getPdo()->prepare($sql);
$sth->execute();
$data = $sth->fetchAll(\PDO::FETCH_OBJ);
PDOException (42000)
SQLSTATE[42000]: Syntax error or access violation: 1055 'mydatabase.acd.call_ref' isn't in GROUP BY
It seems like the only way to get working is to list all the table columns in the group by which is doable but not convenient.
I am able to run the same query directly in the phpmyadmin. So I am not sure why when I run it through Laravel it asks me add all columns.
I have MariaDB installed and both Laravel and PhpMyAdmin are connecting to the same instance. Larvel version is 5.8.5.
Please take a look at full query as I asked question here too but couldn't find any answer - https://laracasts.com/discuss/channels/eloquent/query-runs-ok-in-phpmyadmin-but-dbselect-throws-exception
You should try disabling the strict mode in config/database.php, in mysql connection section.
This is not a Laravel issue, but a logical limitation by MySQL. When you add columns to your results set that are not in the GROUP BY clause, different values could be possible for these columns in the result set.
When executing queries in strict mode (which is on by default), MySQL will not allow this because it could lead to unexpected results. If you turn off strict mode MySQL will use the first result it finds for these columns.
You can turn off strict mode in your Laravel config files in config/database.php. However, it is recommended to change your query instead because of the unpredictability.
A full list of checks in strict mode can be found in the MySQL documentation: https://dev.mysql.com/doc/refman/8.0/en/sql-mode.html#sql-mode-strict
I have installed Laravel 4.8 and also configured Auth. further more,I add two tables called as role and states and tried your pdo code - its perfectly works for me! Here, I put my code.
$sql = "SELECT *
FROM users u
INNER JOIN roles r ON r.id = u.role_id
LEFT JOIN states s ON s.id = u.state_id
WHERE 1=1
GROUP BY u.id
ORDER BY u.id ";
$sth = DB::getPdo()->prepare($sql);
$sth->execute();
$data = $sth->fetchAll(\PDO::FETCH_OBJ);
dd($data);
I am eager to know what is your db structure.

Syntax error: COLUMN isn't in GROUP BY (SQL, Laravel) [duplicate]

This question already has answers here:
Group by not working - Laravel
(8 answers)
Closed 3 years ago.
I get an error in Laravel 5.4 trying to run the following query:
return ReferLinkVisit::where('refer_link_id', $this->id)
->groupBy('ipaddr')
->get()
Error:
SQLSTATE[42000]: Syntax error or access violation: 1055 'database.refer_link_visits.id' isn't in GROUP BY (SQL: select * from `refer_link_visits` where `refer_link_id` = 1 group by `ipaddr`) (View: /resources/views/dashboard/refer/home.blade.php)
Yet I can run the command in phpmyadmin and it will work just fine. I don't get it because I've wrote similar queries a hundred times but for whatever reason this time it just doesn't want to work. I can't figure out what I've done wrong.
Table structure:
You need to disable MySQL strict mode.
Open config/database.php then find mysql array within the connections array.
change the value of strict mode to false.
'connections' => [
...
'mysql' => [
...
'strict' => false, // ensure strict mode is set to false
...
],
...
]
I hope this solves your problem.

Adding pagination to inner join-ed laravel

I am getting error on pagination
$writers = $writer::join('categories','categories.id','=','writers.category_id')
->where([['categories.category_name', '=', $category]])->paginate(1);
Can anyone tell how to paginate an inner join query.
This is the error message I'm getting:
QueryException in Connection.php line 770: SQLSTATE[42000]: Syntax error or access violation: 1140 Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause (SQL: select count(*) as aggregate from writers` inner join categories on categories.id = writers.category_id where (categories.category_name = Comic))
You need to specify GROUP_BY clause on your query
$writers = Writer::join('categories','categories.id','=','writers.category_id')
->where('categories.category_name', '=', $category)
->groupBy('writers.id')
->paginate(1);
UPDATE
If you still get an error, check your config/database.php. Be sure that in mysql settingsstrict = false
UPDATE 2
strict mode works on mysql starting from 5.7. If you have mysql under 5.7, set strict => false. You can check this link for more information:
Strict mode

Executing raw query in laravel 5.3

I am trying to execute some raw queries in laravel 5.3. the queries are simple but im getting errors.
table name: users
columns: id|name|email|phone|created_at|updated_at
my query:
SELECT created_at AS member_since, count(*) as row_count
FROM users
GROUP by MONTH(created_at);
This raw query works fine when I execute this on phpmyadmin. But when I execute this using laravel's database query builder i get error.
SQLSTATE[42000]: Syntax error or access violation: 1055
'query.users.created_at' isn't in GROUP BY
SQLSTATE[42000]: Syntax error or access violation: 1055
'query.users.created_at' isn't in GROUP BY (SQL: select created_at AS
member_since from `users` group by MONTH(created_at))
here is my controller:
$users = DB::table('users')
->select(DB::raw('created_at AS member_since', 'count(*) AS row_count'))
->groupBy(DB::raw('MONTH(created_at)'))
->get();
return response()->json($users);
Please correct me if Im wrong. Are there any better ways to execute raw queries?
First, you need to understand the error you're getting. This question is all about it https://stackoverflow.com/a/38551525/2474872
Now, with that in mind you have two choices
Disable only_full_group_bysetting.
Follow the indications provided
by the error message:
$users = DB::table('users')
->select(DB::raw('created_at AS member_since, count(*) AS row_count'))
->groupBy(DB::raw('MONTH(created_at)'), 'created_at')
->get();
groupBy(DB::raw('MONTH(created_at)'), 'created_at') can also be groupBy(DB::raw('MONTH(created_at), created_at')

Categories