0

I wanted to make a trigger. After creating php make:migration AddTrigger and writing the code when I write php artisan migrate in cmd, I got the following error :

Symfony\Component\Debug\Exception\FatalThrowableError : syntax error, unexpected 'receiver' (T_STRING), expecting ')' at E:\laravel project folder\blood\database\migrations\2019_06_14_173818_add_trigger.php:16

  12|      * @return void
  13|      */
  14|     public function up()
  15|     {
> 16|          DB::unprepared('CREATE TRIGGER amount AFTER INSERT ON 'receiver' FOR EACH ROW
  17|                 BEGIN
  18|                    INSERT INTO 'receipt' ('r_id') VALUES (10);
  19|                 END');
  20|     }

Exception trace:

1 Illuminate\Filesystem\Filesystem::requireOnce("E:\laravel project folder\blood\database\migrations/2019_06_14_173818_add_trigger.php") E:\laravel project folder\blood\vendor\laravel\framework\src\Illuminate\Database\Migrations\Migrator.php:475

2 Illuminate\Database\Migrations\Migrator::requireFiles() E:\laravel project folder\blood\vendor\laravel\framework\src\Illuminate\Database\Migrations\Migrator.php:105

Please use the argument -v to see more details.

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddTrigger extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
     DB::unprepared('CREATE TRIGGER amount AFTER INSERT ON 'receiver' FOR 
EACH ROW
            BEGIN
               INSERT INTO 'receipt' ('r_id') VALUES (10);
            END');
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    DB::unprepared('DROP TRIGGER 'amount'');
}
}

I have used a controller named PageController where I used the function to insert into the receiver.

  • Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php). Invalid code = errors. – Mike Doe Jun 14 '19 at 18:38
  • Possible duplicate of [When to use single quotes, double quotes, and backticks in MySQL](https://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-backticks-in-mysql) – Paul Spiegel Jun 14 '19 at 19:31
  • Don't use single quotes for column and table names. – Paul Spiegel Jun 14 '19 at 19:32
  • Also don't try to use unescaped single quotes in a single quoted PHP string. This isn't going to work in any language I've seen. Any decent editor (IDE) would show you that as syntax error. Even stackoverflow shows you "receiver" in black. Which means - It's not part of the string. – Paul Spiegel Jun 14 '19 at 19:35

1 Answers1

1
  1. you forget to add DB facades add use Illuminate\Support\Facades\DB; to file.

  2. use DB::raw function to execute raw queries and use double quotes for query.

    DB::unprepared(DB::raw("CREATE TRIGGER amount AFTER INSERT ON 'receiver' FOR EACH ROW
                BEGIN
                   INSERT INTO 'receipt' ('r_id') VALUES (10);
                END"));
    
Sunil Kashyap
  • 2,946
  • 2
  • 15
  • 31