Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

database - all grouped retrieved rows are in the first month

I am trying to group retrieved rows from database using by months laravel but all the rows are grouped on the the first month.

$data = Medicals::join("patient_referral", "medicals.trx_id", "=", "patient_referral.trx_id")
    ->where(function ($q) use ($startdate, $enddate) {
        $q->whereBetween('medicals.created_at', [$startdate, $enddate])->get();
    })
    ->leftJoin("patients", "medicals.patients_id", "=", "patients.id")
    ->join("tests", "medicals.tests_id", "=", "tests.id")
    ->get()
    ->groupBy(function (Medicals $item) {
        return Carbon::parse($item->creted_at)->format('m');;
    });

Output enter image description here

Database (Medicals table)

enter image description here

Please help!!!

question from:https://stackoverflow.com/questions/65909846/all-grouped-retrieved-rows-are-in-the-first-month

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

1 Answer

0 votes
by (71.8m points)

You are joining multiple tables. By default, all fields/columns from those tables will be included. We don't really know what created_at field from which table is used.

Medicals::join("patient_referral", "medicals.trx_id", "=", "patient_referral.trx_id")
    ->where(function ($q) use ($startdate, $enddate) {
        $q->whereBetween('medicals.created_at', [$startdate, $enddate])->get();
    })
    // I select all fields from medicals table. You might want to update this later.
    ->selectRaw('medicals.*')
    ->leftJoin("patients", "medicals.patients_id", "=", "patients.id")
    ->join("tests", "medicals.tests_id", "=", "tests.id")
    ->get()
    ->groupBy(function (Medicals $item) {
        return Carbon::parse($item->created_at)->format('m');;
    });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...