Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Tse's avatar
Level 1

Transform a sql query to eloquent query

Tables:

  • courses ( id, name )
  • my_classes (id , name , course_id)
  • my_class_periods ( id, day_of_week, from , to, my_class_id)
  • students ( id, name, card_uid)
  • my_class_student (id, my_class_id, student_id )

Scenario: Student present a card on card reader. The client side send its card uid to server. Server find student by card_uid. and then find which class period match current timestamp.

Assume current is Monday, 15:00. I use the following sql to perform the search.

select * 
from 
    students, my_class_student, my_classes, class_periods, courses 
where 
    students.id = my_class_student.student_id and
    my_classes.id = my_class_student.my_class_id and
    my_classes.id = class_periods.my_class_id and
    classes.course_id = courses.id and
    class_periods.day_of_week = 'Mon' and
    students.card_uid = 'abcd' and
    class_periods.`from` < '15:00' and
    class_periods.`to` > '15:00'

Assume I already have relationship on those models. How can I transform it to eloquent query

0 likes
0 replies

Please or to participate in this conversation.