简体   繁体   中英

How to call the postgress function/stored procedure from laravel controller?

laravel code:

$allUsers=DB::select('call getUsersCount()');

errror:

SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "call" LINE 1: call getUsersCount() ^ (SQL: call getUsersCount()) (Bindings: array ( ))

function in postgress:

CREATE OR REPLACE FUNCTION public."getUsersCount"()
      RETURNS bigint AS
    $BODY$
      SELECT count(*)  FROM users;
    $BODY$
      LANGUAGE sql VOLATILE
      COST 100;
    ALTER FUNCTION public."getUsersCount"()
      OWNER TO postgres;

So what is the best way to call the Postgres functions from laravel?

I don't want to convert this to like table calling. it should be like a stored procedures.

To call the Postgres functions from Laravel controller:

use the following method

$allUsersCount=DB::select('SELECT public."getUsersCount"()');

To call the Postgres functions from Laravel controller:

use the following method

$allUsersCount=DB::select('SELECT * from public.getUsersCount()');

nope this is not working as for laravel 5.8 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM