After investigating the class at:
vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php
You can do the following to generate insert SQL without running them:
use Illuminate\Support\Facades\DB;
function insertRawSql(string $table, array $values): string
{
$builder = DB::table($table);
return $builder->grammar->substituteBindingsIntoRawSql(
$builder->grammar->compileInsert($builder, $values),
$builder->cleanBindings($builder->connection->prepareBindings($values))
);
}
echo insertRawSql('mytable', ['id' => 123, 'name' => 'Test', 'date' => \Carbon\Carbon::now()]);
Give:
insert into "mytable" ("id", "name", "date") values (123, 'Test', '2025-10-24 17:11:00.643')
You just need a connection to a database (sqlite, postgresql, mysql, ...) because of $builder->connection call, but it doesn't run any request so the table may not exist.
Same is possible for update() query.