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

gizmojo's avatar

Strange quote issue creating view table via testbench if contains a quote

I've got this migration file which creates a view table successfully when it's ran normally, but throws an error when ran via testbench.

Tired changing the single to double quotes and escaping with a backslash but keep getting the same error.

  SQLSTATE[HY000]: General error: 1 near "'/'": syntax error (Connection: testing, SQL: CREATE VIEW `urls_view` AS
    SELECT
    TRIM(TRAILING '/' FROM CONCAT(`scheme`, '://', `domain`)) AS full_url
FROM
    urls)

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:574
    570▕             if ($this->pretending()) {
    571▕                 return true;
    572▕             }
    573▕
  ➜ 574▕             $statement = $this->getPdo()->prepare($query);
    575▕
    576▕             $this->bindValues($statement, $this->prepareBindings($bindings));
    577▕
    578▕             $this->recordsHaveBeenModified();

  1   vendor/laravel/framework/src/Illuminate/Database/Connection.php:574
  2   vendor/laravel/framework/src/Illuminate/Database/Connection.php:788
  3   vendor/laravel/framework/src/Illuminate/Database/Connection.php:755
  4   vendor/laravel/framework/src/Illuminate/Database/Connection.php:581
  5   vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php:469
  6   vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:353
  7   tests/database/migrations/2023_08_10_211923_create_urls_view_table.php:15
  8   vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:493
  9   vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:410
  10  vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:419
  11  vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:216
  12  vendor/laravel/framework/src/Illuminate/Console/View/Components/Task.php:37
  13  vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:756
  14  vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:216
  15  vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:181
  16  vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:124
  17  vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php:92
  18  vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:633
  19  vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php:104
  20  vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:36
  21  vendor/laravel/framework/src/Illuminate/Container/Util.php:41
  22  vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:93
  23  vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:37
  24  vendor/laravel/framework/src/Illuminate/Container/Container.php:662
  25  vendor/laravel/framework/src/Illuminate/Console/Command.php:211
  26  vendor/symfony/console/Command/Command.php:326
  27  vendor/laravel/framework/src/Illuminate/Console/Command.php:181
  28  vendor/symfony/console/Application.php:1063
  29  vendor/symfony/console/Application.php:320
  30  vendor/symfony/console/Application.php:174
  31  vendor/laravel/framework/src/Illuminate/Console/Application.php:163
  32  vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:400
  33  vendor/laravel/framework/src/Illuminate/Testing/PendingCommand.php:296
  34  vendor/orchestra/testbench-core/src/helpers.php:39
  35  vendor/orchestra/testbench-core/src/Database/MigrateProcessor.php:68
  36  vendor/orchestra/testbench-core/src/Database/MigrateProcessor.php:43
  37  vendor/orchestra/testbench-core/src/Concerns/WithLoadMigrationsFrom.php:41
  38  vendor/orchestra/testbench-core/src/Concerns/WithLoadMigrationsFrom.php:21
  39  tests/TestCase.php:88
  40  vendor/orchestra/testbench-core/src/Concerns/HandlesDatabases.php:34
  41  vendor/orchestra/testbench-core/src/Concerns/Testing.php:206
  42  vendor/orchestra/testbench-core/src/TestCase.php:71
  43  vendor/orchestra/testbench-core/src/Concerns/Testing.php:99
  44  vendor/orchestra/testbench-core/src/TestCase.php:51
  45  tests/TestCase.php:42
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        DB::statement(self::createView());
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        DB::statement(self::dropView());
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    private static function createView(): string
    {
        return <<<SQL
            CREATE VIEW `urls_view` AS
                SELECT
                TRIM(TRAILING '/' FROM CONCAT(`scheme`, '://', `domain`)) AS full_url
            FROM
                urls
            SQL;
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    private static function dropView(): string
    {
        return <<<SQL
            DROP VIEW IF EXISTS `urls_view`;
            SQL;
    }
};
0 likes
3 replies
tisuchi's avatar
tisuchi
Best Answer
Level 70

@gizmojo I think it's because of heredoc.

To fix the error, you should align the ending SQL; identifier with the beginning of the line,

For example:

    private static function createView(): string
    {
        return <<<SQL
            CREATE VIEW `urls_view` AS
                SELECT
                TRIM(TRAILING '/' FROM CONCAT(`scheme`, '://', `domain`)) AS full_url
            FROM
                urls
SQL;
    }

    private static function dropView(): string
    {
        return <<<SQL
            DROP VIEW IF EXISTS `urls_view`;
SQL;
    }
1 like
gizmojo's avatar

@tisuchi thanks but unfortunately still get the same error. I even tried without the heredoc as a regular string.

If I remove the quotes in the SQL it works and the output query in the error log is correct too. Not sure what to make of it.

1 like
gizmojo's avatar

Strange I've switched to using DatabaseTransactions and it's working fine now

2 likes

Please or to participate in this conversation.