Level 88
You need to use the raw variant for that
->select('firstname', 'lastname', \DB::raw('to_char(birthday, "DD.MM.YYYY") as birthday'))
Documentation: https://laravel.com/docs/6.x/queries#raw-expressions
1 like
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello,
i have this raw sql-query:
SELECT firstname, lastname, to_char(birthday, 'DD.MM.YYYY') as birthday
FROM players
WHERE (extract(year from age(birthday)) + 1) * interval '1 year' + birthday between current_date and now() + '60 days'
ORDER by EXTRACT(Month FROM birthday), EXTRACT(Day FROM birthday)
I have problems to build the select with the query builder.
My first try:
->select('firstname', 'lastname', 'to_char(birthday, 'DD.MM.YYYY') as birthday')
...
doesn't work. How can i use 'to_char' and 'as'??
You need to use the raw variant for that
->select('firstname', 'lastname', \DB::raw('to_char(birthday, "DD.MM.YYYY") as birthday'))
Documentation: https://laravel.com/docs/6.x/queries#raw-expressions
Please or to participate in this conversation.