Reputation: 168
i tried this in laravel, but its not worked.
$doctor_daily=ReportDoctorCompanyDailyTestModal::select('test_id','doctor.first_name','testing_types.testing_name',DB::raw('SUM(test_price) as num'))
->join('testing_types','report_doctor_company_daily_test.test_id','=','testing_types.id')
->join('doctor','doctor.id','=','report_doctor_company_daily_test.doctor_id')
->groupBy('test_id')
->get();
How to write this MySQL Query in Laravel 5.7?
SELECT
report_doctor_company_daily_test.test_id,
testing_types.testing_name,
doctor.first_name,
doctor.last_name,
SUM( report_doctor_company_daily_test.test_price )
FROM
testing_types
INNER JOIN report_doctor_company_daily_test ON testing_types.id = report_doctor_company_daily_test.test_id
INNER JOIN doctor ON report_doctor_company_daily_test.doctor_id = doctor.id
GROUP BY
testing_types.id
in sql this query working. how can i use it to laravel
Upvotes: 0
Views: 702
Reputation: 13404
Try this line:
$doctor_daily=TestingType::join('report_doctor_company_daily_test', 'testing_types.id', '=', 'report_doctor_company_daily_test.test_id')
->join('doctor', 'report_doctor_company_daily_test.doctor_id', '=','doctor.id')
->groupBy('testing_types.id')
->select('report_doctor_company_daily_test.test_id','testing_types.testing_name','doctor.first_name','doctor.last_name',DB::raw('SUM( report_doctor_company_daily_test.test_price )')
->get();
Your mysql version is 5.7+, so check this reference
MySQL 5.7.5 and up implements detection of functional dependence. If the ONLY_FULL_GROUP_BY SQL mode is enabled (which it is by default), MySQL rejects queries for which the select list
so check the config/database.php, if your mysql configuration strict
is true, you need to disabled the ONLY_FULL_GROUP_BY
, add modes like this:
'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'
],
or you can just use any_value method for the selected columns:
PS: any_value does not exist on MariaDB
->selectRaw('ANY_VALUE(report_doctor_company_daily_test.test_id),
ANY_VALUE(testing_types.testing_name),
ANY_VALUE(doctor.first_name),
ANY_VALUE(doctor.last_name),
SUM( report_doctor_company_daily_test.test_price')
Upvotes: 3
Reputation: 191
Go to config/database.php and inside 'mysql' change strict:false
. Now the Group by should work..
Upvotes: 1