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

zyrez's avatar
Level 1

Help Needed: Calling Stored Function from Laravel Controller

Help Needed: Calling Stored Function from Laravel Controller

Hi everyone, first of all sorry for the extension but I think that the only way you could understand is by giving you the whole picture. I hope someone can help me with this issue I'm facing. Here's the context:

  • In my current job, I'm working on a large Laravel project. I'm using the MVC paradigm that needs to display data obtained by calling a stored function in a PostgreSQL database using Oracle SQL Developer.
  • Below, I'll share the code from the controller where I call the function and the code for the function itself. The problem is that when I call the function directly in the database, it returns the correct amount the user owes. However, when I try to pass the same parameters from the Laravel project's controller, it always returns 0. This means that the function in the database does not validate everything correctly to return the amount owed by the user.
  • I should mention that I'm using a syntax to call the function that works correctly for another function in the database. There are differences between the database functions, as the one causing the error can receive a default parameter. I suspect that the error might be related to this, but I'd like to understand how the query is generated from Laravel or how to obtain the correct value. I hope my explanation is clear. Any suggestions or help would be greatly appreciated.

Here is the code for the function call that returns the correct value (and which I used as an example for the one causing the error): `public function tieneTitulo(Request $request){ $ope = $request->operatoria; $barrio = $request->barrio; $adju = $request->adju;

    $query = "SELECT usua400.fun_tiene_titulo(:ope, :barrio, :adju, 'T') as respuesta from dual";
    $resultados = DB::select(DB::raw($query), ['ope' => $ope, 'barrio' => $barrio, 'adju' => $adju]);

    //dd($resultados);
    if (empty($resultados)) {
        return response()->json(['message' => 'Sin datos.']);
    } else {
        return response()->json($resultados[0]);
    }
}`

Here is the code for the function call that always returns 0 (and which I'm trying to debug): `public function consultarAdeuda(Request $request) { $ope = $request->operatoria; $barrio = $request->barrio; $adju = $request->adju; $fecha = Carbon::createFromFormat('d/m/y', '15/07/24')->format('d-m-y');

    try {
        $result = DB::select('SELECT iprodha.fun_adeuda_mont(:ope, :barrio, :adju, :fecha) as respuesta from dual', [
            'ope' => $ope,
            'barrio' => $barrio,
            'adju' => $adju,
            'fecha' => $fecha,
        ]);

        if ($result === null){
            return response()->json(['respuesta' => 'Sin datos.']);
        } else {
            return response()->json(['respuesta' => $result[0]->respuesta]);
        }
    } catch (\Exception $e) {
        return response()->json(['message' => 'Ocurrió un error al ejecutar la consulta: .'], $e->getMessage());
    }
}`

The code of routes: Route::get('juridicos/juicios/consultarAdeuda', [JuiciosController::class, 'consultarAdeuda'])->name('juridicos.juicios.consultarAdeuda');

Here is the code for the Oracle database function in Oracle SQL Developer (you can use any of both links): https://drive.google.com/uc?export=view&id=1MUQd3laGzfw1w2uW5L5X7hPDwmooO-t9 https://drive.google.com/file/d/1MUQd3laGzfw1w2uW5L5X7hPDwmooO-t9/view?usp=drive_link

Thank you very much for your help!

0 likes
4 replies
Tray2's avatar

In your exception handler you are doing the same thin regardless of the exception, so there I would remove the no_data_found and the too_many_rows and just use others. Then I would change the return type to VARCHAR2 and use

EXCEPTION WHEN OTHERS THEN
	RETURN SQLERRM;

That way I will know what went wrong, but my guess is that fecha_al isn't a proper date. I would pass it as VARCHAR2 and then convert it to date in the function.

1 like
martinbean's avatar

@zyrez You’re “blackhole-ing” exceptions. You catch them… and then throw it away to just return a generic error message. You’ve lost all contextual information that would help you actually diagnose the issue, such as what type of exception was thrown and its stack trace.

Don‘t catch exceptions. This is what the execution handler is for. It will catch exceptions, and actually write helpful information to your error log instead of just presenting the user with “an error occurred”, who’s then going to tell you there was an error, and you’re going to go, “Sorry, don‘t know anything about it as nothing reached our error logs because I just wrapped everything in a try/catch and discarded all useful information.”

Please or to participate in this conversation.