Laravel 4 - Cannot catch database exception in seed or migration class

前端 未结 2 1493
感情败类
感情败类 2021-01-13 17:13

Laravel 4 with MySql db. For some reason, I cannot catch DB exceptions (Illuminate\\Database\\QueryException) inside a seed or migration class: the code nev

相关标签:
2条回答
  • 2021-01-13 17:36

    You must catch "Illuminate\Database\QueryException"

    try {
        $data = array('id' => 1, 'name' => 'foo');
        DB::table('table')->insert($data);
    }
    catch (\Illuminate\Database\QueryException $e) {
        $this->command->error("SQL Error: " . $e->getMessage() . "\n");
    }
    
    0 讨论(0)
  • 2021-01-13 17:43

    Thanks to php exceeds execution time on throwing an exception in recursion I finally understood the problem.

    Apparently it's related to XDebug, and it occurs only on PHP on Mac OS X (I tried several PHPs on Windows, but never ran into this problem), and only when running a migration or seed by CLI (e.g. php artisan db:seed --class=className).

    It's related to these XDebug settings:

    xdebug.var_display_max_depth = -1
    xdebug.var_display_max_children = -1
    xdebug.var_display_max_data = -1
    

    I don't exactly know why, but this caused QueryException to never be catched, and PHP showed me the entire exception stack.

    If I comment these options, or set them to a more appropriate value (e. g. 10) or if I disable XDebug (not a good solution), everything runs ok, and inside migrations/seeds I can catch QueryExceptions.

    0 讨论(0)
提交回复
热议问题