Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

omontes's avatar

sql to eloquent

Hello friends, I have problems to extract the year from a date, grouped by date and sort by date using Eloquent

The sql statement I use is:

notifications = DB::select("SELECT EXTRACT(YEAR FROM created_at) AS a,*
                            FROM notifications
                            WHERE volcano_id =".$id."
                            group by created_at,id
                            ORDER BY created_at");

and using eloquent is this but I can not work

$notifications = Notification::where('volcano_id', '=', $id)->get();

using postgresql

0 likes
1 reply
RayRutjes's avatar

Hi @omontes, the EXTRACT keyword is very MySQL specific and should not be used directly. You should always try to use some kind of abstracted way to query your data so you can switch your database at anytime. Moreover why don't you extract that value later on directly in your application ?

Please or to participate in this conversation.