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

azegurb's avatar

Querying Postgresql stored function works on pure php but not works with Laravel

Hi all, Recently I have created stored function in Postgres SQL which deals about with hashing passwords. When used via PgAdmin tool or pure PHP it works like a charm as stated below. But when i call it from laravel as stated below.

$token=\DB::select("SELECT * from "horeca_user".update_token('$type', '$user_id', '$token')");

It Throws error

local.ERROR: SQLSTATE[42883]: Undefined function: 7 ERROR: function digest(character varying, unknown) does not exist LINE 1: SELECT encode(digest(token, 'sha256'), 'hex')

When i called same stored function from pure PHP using PDO connection it works.

$db = new PDO("pgsql:dbname=Horecami;host=127.0.0.1", "postgres", "1" ); $sql = 'Select horeca_user.update_token(?, ?, ?)'; $stmt = $db->prepare($sql); $type=1; $id=1; $z='az'; $stmt->bindValue(1, $type, PDO::PARAM_INT); $stmt->bindValue(2, $id, PDO::PARAM_INT); $stmt->bindValue(3, $z, PDO::PARAM_STR); $stmt->execute(); $res=$stmt->fetchAll();

My Postgre SQL function full code was stated below.

CREATE OR REPLACE FUNCTION horeca_user.update_token(tp integer,userid integer,token character varying) RETURNS character varying LANGUAGE 'plpgsql' VOLATILE PARALLEL UNSAFE COST 100 AS $BODY$ DECLARE uid INTEGER; DECLARE tok character varying; BEGIN uid:=0; Select count(*) as total from horeca_user.user_tokens where user_id=$2 into uid;

if uid=0 THEN
   Insert into horeca_user.user_tokens(id, user_id, tip) values(, , );
 else
   Update horeca_user.user_tokens set id=, tip= where user_id=;
 END IF;

 SELECT encode(digest(token, 'sha256'), 'hex') as tt into tok;

 return tok;

END

$BODY$;

Any help greatly appreciated. About is two weeks i am struggling with this problem.

0 likes
0 replies

Please or to participate in this conversation.