Mehravish Temkar Mehravish Temkar - 4 months ago 40
MySQL Question

How to write orWhereYear() in Laravel

I have two datetime columns in my table - from_date and to_date. I need to get records where the year of either from_date or to_date is current year. I am trying to get the records using whereYear and I am stuck at the OR condition

$table_records = TableName::whereYear('from_date' , '=', date('Y'))->orWhereYear('to_date' , '=', date('Y'))->orderBy('from_date')->get();

But of course there's no orWhereYear in Laravel I guess. Can someone please help me with the query.


One way of doing it would be like this:

$table_records = TableName::whereYear('from_date' , '=', date('Y'))
    ->whereYear('to_date' , '=', date('Y'), 'or')