How to query only by month on date input and ignore the year and day considering indexing or performance on database in laravel

为君一笑 提交于 2019-12-25 07:54:50

问题


I have this table

training_schedules

  • id
  • course_reference
  • start_date(date)
  • end_date(date)
  • year_id(int, fk)

on my form, I have dropdownlist for the month, and another one for the year. Now as you can see there is a mismatch on the month from form and database column. How can I filter the start_date or end_date column using only the integer value from the dropdownlist and ignore the day and year part of the db column?

I saw some examples but using raw query and using date functions of database which is not good if you consider performance because it is not indexable.

Can I achieve this using Eloquent/Fluent Api of Laravel?


回答1:


If you are using mysql u can use the month() function to get month from date

For further clarification please visit the link



来源:https://stackoverflow.com/questions/22470657/how-to-query-only-by-month-on-date-input-and-ignore-the-year-and-day-considering

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!