0

I simply need to calculate a new field date in select in eloquent... but it does not seems to work although if I copy the mysql query in phpmyadmin it works.

 PV_Task::select("*,IF(date_start<'".$today."','".$today."',date_start) AS date")->whereRaw("project = 'xxx'")->orderByRaw('date asc, date_start asc, phase asc, sortorder asc')->get();

 SQLSTATE[42S22]: Column not found: 1054 Unknown column 
 'IF(date_start<'2024-03-10','2024-03-10',date_start)' in 'field list' 

 select
   *,
   `IF(date_start<'2024-03-10','2024-03-10',date_start)` as `date`.   
 from
   `table`
 where
   (project = 'xxx')
 order by
   id asc

Of course, I can do it in not-eloquent... but I want to learn... Is it possible?

 selectRaw(as above) works fine
3
  • 1
    please show your eloquent query Commented Mar 10, 2024 at 10:44
  • 2
    Maybe selectRaw? Commented Mar 10, 2024 at 10:48
  • @PaulGodard An accessor attribute function for date might be more appropriate here. Commented Mar 11, 2024 at 14:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.