Laravel 4 database statement create trigger

database, laravel, mysql, pdo, triggers

Solution

Use DB::unprepared(), works like a charm.

Problem

During the creation process of my app's migration files, I noticed that Laravel does not support database triggers! I have come to terms with the fact that I need to execute a query statement to accomplish this, but that is also giving me troubles.. Here is a code snippet from my app: ``` Schema::create('users', function ($table) { $table->increments('id'); $table->string('uuid', 36); $table->string('email', 255); $table->string('password', 255); }); DB::statement('CREATE TRIGGER users_trigger_uuid BEFORE INSERT ON users FOR EACH ROW SET NEW.uuid = UUID()'); ``` When I run artisan migrate it gives me the following error: [Exception] SQLSTATE[HY000]: General error: 2030 This command is not supported in the prepared statement protocol yet (SQL: CREATE TRIGGER users_trigger_uuid BEFORE INSERT ON users FOR EACH ROW SET NEW.uuid = UUID()) (Bindings: array ( )) Are there any solutions apart from creating my own PDO object and executing the query outside of Laravel? Is this a MySQL exception or a Laravel exception? EDIT Judging by the exception, it's clear that prepared statements don't support the creation of triggers.. not sure why but would love some insight. To get around this I just ran my own PDO query. ``` $default_driver = Config::get('database.default'); $connection_info = Config::get('database.connections.' . $default_driver); $conn = new PDO('mysql:host=' . $connection_info['host'] . ';dbname=' . $connection_info['database'], $connection_info['username'], $connection_info['password']); $conn->query('CREATE TRIGGER users_trigger_uuid BEFORE INSERT ON ' . $connection_info['prefix'] . 'users FOR EACH ROW SET NEW.uuid = UUID()'); ```

Original source

Related problems