Joyce
Joyce

Reputation: 57

Using Laravel DB object to run postgresql functions with input parameters

I have some postgresql functions that I run in the form:

SELECT * FROM pgfunction(1,2,3);

This enables me to filter / order / group the results:

SELECT * FROM pgfunction(1,2,3) WHERE value > 10;

How could I pass the parameters to the postgresql function in laravel using prepared statements in order to filter the results in laravel. Example:

DB::table('pgfunction',array(1,2,3))->where('value','>','10');

Is this possible?

Upvotes: 0

Views: 2160

Answers (1)

fideloper
fideloper

Reputation: 12293

DB::raw will create a raw SQL string, but not use it as a query. You may need to use a DB::select() to run a "raw sql" query.

$fnc = sprintf("pgfunction(%s)", implode(',', array(1,2,3));

DB::select()->from(DB::raw($fnc))->where('value', '>', 10);

Note: This is untested on my end. Hope it helps. See the docs on using Raw Expressions for more information on using that.

Upvotes: 2

Related Questions