Escape raw SQL queries in Laravel 4

后端 未结 7 992
别那么骄傲
别那么骄傲 2020-12-15 16:23

How does one go about escaping parameters passed to a raw query in Laravel 4? I expected something like DB::escape() (which rings a bell from Laravel 3) and als

7条回答
  •  無奈伤痛
    2020-12-15 16:29

    Two answers here, that I use, have less verbose solutions built into the DB facade.

    First, value quoting:

    // From linked answer
    DB::connection()->getPdo()->quote("string to quote");
    // In the DB facade
    DB::getPdo()->quote('string to quote');
    

    Second, identifier quoting (table and column names):

    // From linked answer
    DB::table('x')->getGrammar()->wrap('table.column');
    // In the DB facade
    DB::getQueryGrammar()->wrap('table.column');
    

提交回复
热议问题