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.
Related
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
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.
This question already has answers here:
Duplicate a document in MongoDB using a new _id
(7 answers)
Closed 4 years ago.
I'm somewhat new to MongoDB and I'm trying to change a primary key on a record via PHP. When I try to do so, I get the following error:
PHP Fatal error: Uncaught exception 'MongoWriteConcernException' with message '12.34.56.78:27017: After applying the update to the document {_id: ObjectId('5b077638aaf909b2528b51fd') , ...}, the (immutable) field '_id' was found to have been altered to _id: ObjectId('58b5882c4658a3de67ce2907')'
The new id (58b5882c4658a3de67ce2907) has been confirmed as unique.
This is the PHP code that I'm using to attempt this change:
$contactsQuery_for_update = array('center' => 12345,'_id' => new
MongoId('5b077638aaf909b2528b51fd'));
$collection_contacts->update(
$contactsQuery_for_update,
[ '$set' => [ '_id' => new MongoId('58b5882c4658a3de67ce2907') ]]
);
Has anyone else been able to do this successfully? Any direction is appreciated.
Thanks!
I think the error message answers your question by telling you it's immutable. i.e. it can't be changed.
I guess if you have good reason to change the field, you'd have to delete the old record, and insert a new one with your preferred id.
This question already has answers here:
Group by not working - Laravel
(8 answers)
Closed 3 years ago.
I have tried solutions in other questions but that is not solving my issue. I have made strict from true to false. Used modes
'modes' => [
'STRICT_TRANS_TABLES',
'NO_ZERO_IN_DATE',
'NO_ZERO_DATE',
'ERROR_FOR_DIVISION_BY_ZERO',
'NO_AUTO_CREATE_USER',
'NO_ENGINE_SUBSTITUTION'
]
as well. But I am getting the same error.
$orders = Order::orderBy('id', 'desc')->groupBy('order_id')->get();
dd($orders);
This is throwing the error
Syntax error or access violation: 1055 'my_db.orders.id' isn't in GROUP BY (SQL: select * from orders group by order_id order by id desc)
(I am using Laravel 5.6)
In config/database.php
Change 'strict' => true To 'strict' => false
and clear the cache
php artisan config:cache
OR In MySql settings change
mysql > SET GLOBAL sql_mode=(SELECT REPLACE(##sql_mode,'ONLY_FULL_GROUP_BY',''));
Short answer
This is a MySQL issue that can be resolved changing Laravel config as #Rp9 wrote, modifying strict => true to strict => false in config/database.php.
Long Answer
MySQL 5.7 introduced something we are calling "strict mode," which is a combination of new modes that, in sum, make MySQL process your queries a little more strictly than before.
"Strict mode" is a list of modes MySQL 5.7 enables by default, comprised of the following modes:
ONLY_FULL_GROUP_BY
STRICT_TRANS_TABLES
NO_ZERO_IN_DATE
NO_ZERO_DATE
ERROR_FOR_DIVISION_BY_ZERO
NO_AUTO_CREATE_USER
NO_ENGINE_SUBSTITUTION
So, what if you want to disable / enable just few of this stricts mode? Laravel help us.
'connections' => [
'mysql' => [
// Ignore this key and rely on the strict key
'modes' => null,
// Explicitly disable all modes, overriding strict setting
'modes' => [],
// Explicitly enable specific modes, overriding strict setting
'modes' => [
'STRICT_TRANS_TABLES',
'ONLY_FULL_GROUP_BY',
],
]
]
This might be obvious, and I may be completely missing the point, but I've got a table which contains multiple rows with user_id, site_id, e.g.
ldstaff | ld_site
____________________
3 1
3 2
4 1
A MySQL query originally written by an old dev got the staff id find their name and then concat all the sites together to make something like this.
StaffID | Name | sites
3 Dave 1,2
This query was written initially as
SELECT `ld_staff` as staffID ,a.name,GROUP_CONCAT(`ld_site`) as sites FROM `lead_distribution` l
LEFT JOIN users a ON a.crm_id = l.ld_staff
WHERE ld_enabled = 1 AND
`account_type` = 1 AND `active` = 1 AND `no_more` = 0 AND network_team > 0 AND renewal_team = 0
GROUP BY `ld_staff`
However whenever we try to write it in Laravel as part of a new system to get the same results we have the error SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #2 no matter how we try to tamper with the query it always comes back to this. Reading up it seems sql_mode only_full_group_by doesn't allow this type of query, so I've changed sql_mode to = " " and this error persists.
Query
$data = DB::table('lead_distribution')
->join('users', 'ld_staff', '=', 'users.crm_id')
->select("lead_distribution.ld_staff", 'users.name',
DB::raw("GROUP_CONCAT(`ld_site`) as sites"))
->where('users.account_type', '=', 1)
->where('users.active', '=', 1)
->where('users.no_more', '=', 0)
->where('users.network_team', '>', 0)
->where('users.renewal_team', '=', 0)
->groupBy('lead_distribution.ld_staff')
->get();
#jaysingkar
Answer fixed it, my stupid mistake for missing it.
in config/database.php
Change 'strict' => true To 'strict' => false
config/database.php
Change 'strict' => true To 'strict' => false
and clear the cache
php artisan config:cache
then issue will be resolved.
I encountered this problem and got it solved by including the aggregate function in the GROUP BY clause.
I forgot where I have read this, but Aggregate functions must be included in the specified sql_mode only_full_group_by mode.
So, try using
->groupBy(['lead_distribution.ld_staff','sites']) or ->groupBy(['lead_distribution.ld_staff','users.name','sites'])
A handy tip
Try dumping the SQL Query produced by laravel by replacing ->get() with ->toSql() then try running the query produced by laravel and see if the error occurs.