In my MySQL table have four columns for date, like from_year, from_month, to_year, to_month. When I will apply the below code to fetch data it do not work properly.
<?php if(!empty($request->from_year)) { $query->where([['from_year', '>=', $request->from_year]]); } if(!empty($request->from_month)) { $query->where([['from_month', '>=', $request->from_month]]); } if(!empty($request->to_year)) { $query->where([['to_year', '<=', $request->to_year]]); } if(!empty($request->to_month)) { $query->where([['to_month', '<=', $request->to_month]]); } ?>
I want to apply the MySQL CONCAT method to fetch data properly. How can I apply it?
Apply this code:
<?php if(!empty($request->from_year) && !empty($request->from_month)) { $query->where(DB::raw("CONCAT(from_year,'-',from_month)"), '>=', $request->from_year.'-'.$request->from_month); } if(!empty($request->to_year) && !empty($request->to_month)) { $query->where(DB::raw("CONCAT(to_year,'-',to_month)"), '<=', $request->to_year.'-'.$request->to_month); } ?>
Hope it will be helpful for you.
Question and answer is powered by AnsPress.io