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

gmanish's avatar

DB::insert - returns no error, yet no inserts result from statement

I have created a backup of a given table using the following raw queries:

    DB::statement("DROP TABLE IF EXISTS answers_bup");
    DB::statement("CREATE TABLE answers_bup AS TABLE answers");

The answers table has the following schema:

    CREATE TABLE answers
    (
      id uuid NOT NULL,
      user_id uuid NOT NULL,
      survey_id uuid NOT NULL,
      question_id uuid NOT NULL,
      answer character varying(255) NOT NULL,
      created_at timestamp(0) without time zone NOT NULL,
      updated_at timestamp(0) without time zone NOT NULL,
    }

Now, in order to restore a single row, from the answers_bup table to the answers table. I wrote the following DB::insert:

$void = DB::insert("INSERT INTO answers
                SELECT
                '?'::uuid AS id,
                '?'::uuid AS user_id,
                '?'::uuid AS survey_id,
                '?'::uuid AS question_id,
                answer,
                created_at,
                updated_at
            FROM
                answers_bup
            WHERE
                id='?'::uuid", [
    $newId,
    $user_id,
    $survey_id,
    $question_id,
    $answer->id
]);

Basically, I only need to copy over three fields from the answers_bup table - answer, created_at and updated_at. The others, have to be assigned new values, hence the above statement.

When I run this code fragment, I get no errors. Yet, the insert does not happen. The answers table remains empty.

Could anyone help me understand what might be wrong here?

PS: I am working with a Postgres database v9.1

0 likes
4 replies
Qlic's avatar

@gmanish try this

try {
    $void = DB::insert("INSERT INTO answers
                SELECT
                '?'::uuid AS id,
                '?'::uuid AS user_id,
                '?'::uuid AS survey_id,
                '?'::uuid AS question_id,
                answer,
                created_at,
                updated_at
            FROM
                answers_bup
            WHERE
                id='?'::uuid", [
    $newId,
    $user_id,
    $survey_id,
    $question_id,
    $answer->id
]);
} catch (QueryException $e) {
    dd($e);
}
gmanish's avatar

@Qlic I've tried that. No exceptions are thrown either. After the insert has executed, $void is empty.

Qlic's avatar

What happens when you manually execute that SQL in phpmyadmin? Also, install Laravel-Debugbar package, gives alot of usefull information

gmanish's avatar

@Qlic It works just fine in pgAdmin SQL view. I am using a Postgres database. Really sorry, this information should have been part of the question.

Please or to participate in this conversation.