Escape raw SQL queries in Laravel 4

后端 未结 7 995
别那么骄傲
别那么骄傲 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:37

    I found this question when looking for generic sql escaping in Laravel. What I actually needed though was table/column name escaping. So, for future reference:

    /**
     * Quotes database identifier, e.g. table name or column name. 
     * For instance:
     * tablename -> `tablename`
     * @param  string $field 
     * @return string      
     */
    function db_quote_identifier($field) {
      static $grammar = false;
      if (!$grammar) {
        $grammar = DB::table('x')->getGrammar(); // The table name doesn't matter.
      }
      return $grammar->wrap($field);
    }
    

提交回复
热议问题