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
365 views
in Technique[技术] by (71.8m points)

php - Laravel 7: getting rows between two dates where the column is datetime

I'm having trouble building an eloquent query wherein I can recreate this sql command: The date_created column is datetime.

SELECT * FROM covidchecklist cc 
LEFT JOIN patient_appointment pa ON cc.appointment_id = pa.appointment_id 
LEFT JOIN users pat ON pa.user_id = pat.id 
WHERE DATE_FORMAT(cc.date_created,'%Y-%m-%d') BETWEEN '2021-01-27' AND '2021-01-27'

What I have right now:

CovidChecklist::
leftJoin('patient_appointment', 'covidchecklist.appointment_id', '=', 'patient_appointment.appointment_id')
        ->leftJoin('users as patient', 'patient_appointment.user_id', '=', 'patient.id')
        ->whereRaw(' DATE_FORMAT(covidchecklist.date_created,"%Y-%m-%d") BETWEEN '.'$this->date_from'.' AND '.'$this->date_to'.' ')
        ->select(
            *select stuff*
         )
        ->get();

I am using whereRaw since I can't get this to work:

->whereBetween('covidchecklist.date_created', [$this->date_from, $this->date_to])
question from:https://stackoverflow.com/questions/65929524/laravel-7-getting-rows-between-two-dates-where-the-column-is-datetime

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

1 Answer

0 votes
by (71.8m points)

Try this, mate

->whereBetween(DB::raw('DATE_FORMAT(covidchecklist.date_created,"%Y-%m-%d")'), [$this->date_from, $this->date_to])

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

...