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

ynoth25's avatar

Call postgres function from Laravel Query builder

I'm having trouble calling postgres function from laravel query builder, I use DB::raw(function_name(param1, param2, param3)) as calling format.

It always says function does not exists, why? I use Laravel 8.

0 likes
7 replies
tykus's avatar

raw takes a string; do you use a string?

DB::raw('function_name(param1, param2, param3)') 
tykus's avatar

@ynoth25 it does not work is not a very useful response; how does it "not work"; what error message(s) do you see?

ynoth25's avatar

@tykus , oh sorry.

Object of class Illuminate\Database\Query\Builder could not be converted to int

i think it does not return a result.

tykus's avatar

@ynoth25 that is a different error message; what is your code for the completed query?

ynoth25's avatar

@tykus

DB::table('ws_dtr')->select( DB::raw("dtr_subtotal($student_id, $start_date, $end_date)") );

tykus's avatar

@ynoth25 you did not finish your query. You can get stdClass object(s) using first (single instance) or get (collection of instances) methods; or if there is an alias for the function result, use value:

$foo = DB::table('ws_dtr')->selectRaw("dtr_subtotal($student_id, $start_date, $end_date) as foo") ->first();

Or, (I don't know if an alias like this is valid PostGRES syntax, but this is the general idea)


DB::table('ws_dtr')->selectRaw("dtr_subtotal($student_id, $start_date, $end_date) as foo") ->value('foo');

Please or to participate in this conversation.