Laravel GROUP BY without disabling strict mode - php

I was trying to run this query in laravel:
$siswa = DB::table('siswa')
->join('pengambilankelas', 'siswa.id_siswa', '=', 'pengambilankelas.id_siswa')
->join('subcpmkpengambilan', 'pengambilankelas.id_pengambilanKelas', '=', 'subcpmkpengambilan.id_pengambilanKelas')
->join('tesformatif', 'subcpmkpengambilan.id_subcpmkpengambilan', '=', 'tesformatif.id_subcpmkpengambilan')
->select('siswa.*', 'tesformatif.*', DB::raw('max(tesformatif.nilai_tesFormatif) as max_nilai'))
->groupBy('siswa.id_siswa')
->where('pengambilankelas.id_kelas','=', $id_kelas)
->where('subcpmkpengambilan.id_subCPMK', '=', $currentSubcpmk->id_subCpmk)
->where('subcpmkpengambilan.status_subcpmkpengambilan', '=', 3)
->get();
then i got this error:
SQLSTATE[42000]: Syntax error or access violation: 1055 'dil.siswa.identitas_siswa' isn't in GROUP BY (SQL: select `siswa`.*, `tesformatif`.*, max(tesformatif.nilai_tesFormatif) as max_nilai from `siswa` inner join `pengambilankelas` on `siswa`.`id_siswa` = `pengambilankelas`.`id_siswa` inner join `subcpmkpengambilan` on `pengambilankelas`.`id_pengambilanKelas` = `subcpmkpengambilan`.`id_pengambilanKelas` inner join `tesformatif` on `subcpmkpengambilan`.`id_subcpmkpengambilan` = `tesformatif`.`id_subcpmkpengambilan` where `pengambilankelas`.`id_kelas` = 5 and `subcpmkpengambilan`.`id_subCPMK` = 6 and `subcpmkpengambilan`.`status_subcpmkpengambilan` = 3 group by `siswa`.`id_siswa`)
Query run fine outside laravel. this post tell me to disable strict mode
and it works.
is there a better way without disabling strict mode? is it even save to do so?
ps. I'm sorry the database is in indonesian.

You may have the sql_mode ONLY_FULL_GROUP_BY enabled. To disable it run in the mysql console the following command:
mysql > SET GLOBAL sql_mode=(SELECT REPLACE(##sql_mode,'ONLY_FULL_GROUP_BY',''));
References
Disable ONLY_FULL_GROUP_BY

Related

Laravel GroupBy work in mysql but throws error in MariaDB

I'm Running the following query without any problem in MySQL (5.7.21) Database but this query throws error on my hosting server database which is MariaDB (10.1.47-MariaDB) and I don't have super permission to change the sql mode.
$projects = V2Project::leftJoin('v2_project_locations as v2pl', 'v2pl.project_id', 'v2_projects.id')
->leftJoin('v2_locations as v2l', 'v2l.id', 'v2pl.location_id')
->selectRaw('v2_projects.id, v2_projects.name, group_concat(v2l.name) as locations')
->withDepartmentInMind()
->onlyAuthorizedObjects();
if($by_current_step == true) {
$projects = $projects->where('current_step', 'plan');
}
if($search_val) {
$projects = $projects->whereIn('v2_projects.id', $search_val);
}
$projects = $projects->groupBy('v2_projects.id')->paginate(10);
$projects->appends(Input::except('page', '_token'));
Error:
SQLSTATE[42000]: Syntax error or access violation: 1055 'pm_form_moe.v2_projects.name' isn't in GROUP BY (SQL: select v2_projects.id, v2_projects.name from `v2_projects` group by `v2_projects`.`id`)
I also tried to add every column, but I need the functionality the same as mySQL without adding all columns.
Read about ONLY_FULL_GROUP_BY, then decide what to remove from your SELECT or what to add to the GROUP BY. Don't worry about setting the flag; your code is bad and needs to be fixed. And it was bad in the older version when it did not complain.
(If you show us the generated SQL, we can discuss it further.)

Laravel Database Strict Mode

I'm little wonder please help me out.
My query is :- Invoice::join('orders', 'orders.invoice_id', '=', 'invoices.id')->groupBy('invoices.id')->get();
Then I have got an error: Syntax error or access violation: 1055 'invoices.reference_number' isn't in GROUP BY
When I updated my query according to error then the query is:-
$invoices = Invoice::join('orders', 'orders.invoice_id', '=', 'invoices.id')->groupBy('invoices.id','invoices.reference_number','invoices.customer_address_id','invoices.country_id','invoices.paid_at','invoices.due_date','invoices.created_at','invoices.updated_at','invoices.deleted_at','orders.id','orders.currency_id','orders.user_id','orders.customer_id','orders.created_at','invoices.updated_at')->get();
Now query working but in that case, I can't use select(), It returns columns who is in the groupBy()
So I did google then found if I do database strict mode off then it will work and then I did and it's working good, But I don't want strict mode turned off.
Also, i did the study about database modes https://dev.mysql.com/doc/refman/5.5/en/faqs-sql-modes.html https://dev.mysql.com/doc/refman/5.5/en/sql-mode.html
Then I found query:- SELECT ##GLOBAL.sql_mode; SELECT ##SESSION.sql_mode;
But the result is blank
If I do in the database - select * from invoices inner join orders on orders.invoice_id = invoices.id where invoices.deleted_at is null group by invoices.id
Then it's working fine means no mode validations trough mySQL it has configured side of Laravel
Also, i haven't found related database modes in laravel documentation
So help me out if I did wrong something.
And i also want to know who's database strict mode validation is enabled in laravel because I have wonder what strict validation giving me an error.
Change this line in mysql array of config/database.php
'strict' => true,
To
'strict' => false,
Where this occurs add this top of the function
public function index() {
DB::statement("SET sql_mode = '' ");
# rest of your Joins
}
run this query after setting connection in database file
SET sql_mode = false;

GroupBy Error in Laravel Eloquent Builder

I dont want to believe there is a bug in laravel 5.5 cos i am amazed how i keep getting errors on the Eloquent Query Builder when ever i use the groupBy in my query.
Tried converting this SQL that works seamlessly to Eloquent and i keep getting Errors on the groupBy..
Had a similar issue with an SQL which i complained here and till now i have not gotten a perfect answer for it
open issue here
SELECT * FROM arm_articles
INNER JOIN arm_interest ON arm_articles.article_category = arm_interest.category_id
WHERE arm_articles.article_contributor_id='1322'
GROUP BY arm_articles.article_id
ELOQUENT VERSION THAT RETURNS ERROR
$contributor_id =1322;
DB::table('arm_articles')
->join('arm_interest', function($join) {
$join->on('arm_articles.article_category','=','arm_interest.category_id');
})
->having('arm_articles.article_contributor_id', '=', $contributor_id)
->groupBy('arm_articles.article_id')->get()
ERROR MESSAGE
SQLSTATE[42000]: Syntax error or access violation: 1055 'knowledge_db.arm_articles.id' isn't in GROUP BY (SQL: select * from 'arm_articles' inner join 'arm_interest' on 'arm_articles'.'article_category' = 'arm_interest'.'category_id' group by 'arm_articles'.'article_id' having 'arm_articles'.'article_contributor_id' = 1322)
So like i said b4 its really weared why i will get the error in the first place as adding ->toSql to the query returns the right SQL.
Add this configuration to your mysql database config in config/database.php:
'mysql' => [
....
'strict' => false,
//'strict' => true,
],
Edit sql_mode in your mysql config file (my.ini or my.cnf)
[mysqld]
sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Restart your MySQL

Laravel Eloquent join error

I am tryng to make a join in laravel 5.1
here is my code
Venta::where('dbo.Venta.mov', 'some' )
->where('dbo.Venta.Estatus', 'C')
->join('dbo.Cte', 'dbo.Cte.Client', '=', 'dbo.Venta.Cliente')
->where('dbo.Venta.Suc', '=','1')
->paginate(10);
but i have the error:
SQLSTATE [42000]: [Microsoft] [ODBC Driver for SQL Server 11] [SQL Server] The 'Client' column has been specified multiple times for 'temp_table'.
Can somebody please help me solve this issue?

Pagination not working in Laravel 5.1

I am trying to set pagination in Laravel 5.1, here is what I am trying to do:
$bridal_requests_data = \DB::table('bridal_requests')->leftJoin('audiences', function($join) {
$join->on('bridal_requests.id', '=', 'audiences.request_id');
})
->orderBy('bridal_requests.id', 'DESC')->paginate('15', array('bridal_requests.*'));
it is giving the following error:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*) as aggregate from bridal_requests left join audiences on bridal_requests' at line 1 (SQL: select count(bridal_requests.*) as aggregate frombridal_requestsleft joinaudiencesonbridal_requests.id=audiences.request_id`)
As stated above, it is not working for bridal_requests.*, but if I use brial_requests.id then it works, but I need to use get all data from bridal_requests table.
You should use:
$bridal_requests_data = \DB::table('bridal_requests')->select('bridal_requests.*')->leftJoin('audiences', function($join) {
$join->on('bridal_requests.id', '=', 'audiences.request_id');
})
->orderBy('bridal_requests.id', 'DESC')->paginate(15);

Categories