Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
192 views
in Technique[技术] by (71.8m points)

php - How to write Laravel orderBy query?

i tried this in laravel,but its not worked.

$doctor_daily=ReportDoctorCompanyDailyTestModal::select('test_id',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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

your question is not clear for orderBy. Based on your question :

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_t
ypes.id

Answer :

$test = ReportDoctorCompanyDailyTestModal::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)')
    )
    ->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');

dd($test->toSql());

Result :

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`

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...