I have data stored in a database with a column data type of array. I can get the value in PostgreSQL as follows:
select id, student, subject[1] as first_subject, subject[array_length(subject, 1)] as last_subject
from tableinfo
So, I need to get the first and last value of the array in the Laravel eloquent query. But using select in eloquent doesn’t seem to work.
Eloquent query:
DB::Table('tableinfo')
->select('id', 'name', 'subject[array_length(subject, 1)] as last_subject')
->get();
>Solution :
You can not use custom/raw select using select method, but you can use selectRaw method like below:
DB::Table('tableinfo')
->select('id', 'name')
->selectRaw('subject[array_length(subject, 1)] as last_subject')
->get();
above code will generate query like below:
select "id", "name", subject[array_length(subject, 1)] as last_subject from "tableinfo"
