Instead of
DB::unprepared(
DB::raw(" ... "))
Can you try to use
DB::statement('CREATE ...')
DB::statement('DROP ...')
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have created and populated a temporary MySQL table with the following:
$tbl = DB::unprepared(
DB::raw("CREATE TEMPORARY TABLE IF NOT EXISTS temp1 AS (".$sql.")")
);
I have confirmed that it exists and contains the expected data by querying it like this:
DB::table('temp1')->first();
However, when I try to drop it at the end of the script using this:
$drop = DB::unprepared(
DB::raw("DROP TEMPORARY TABLE temp1;")
);
... I get the error:
local.ERROR: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'j2.temp1' doesn't exist
I know that MySQL removes temporary tables, so perhaps I don't need to worry about attempting to remove it manually, but I just can't understand why the query to drop the temp table returns an error saying that it doesn't exist when a select query at the same point in the script returns data.
Should I skip any attempt to drop it and just move on with my life? Would doing this present any risk?
Please or to participate in this conversation.