2017-12-16 17 views
4

나는이 방식에의하지 바인드 쿼리를 얻을 수 있습니다 :Laravel - 바인드 매개 변수로 쿼리를 얻는 방법?

\DB::enableQueryLog(); 
$items = OrderItem::where('name', '=', 'test')->get(); 
$log = \DB::getQueryLog(); 
print_r($log); 

출력은 다음과 같습니다

(
    [0] => Array 
     (
      [query] => select * from "order_items" where "order_items"."name" = ? and "order_items"."deleted_at" is null 
      [bindings] => Array 
       (
        [0] => test 
       ) 
      [time] => 0.07 
     ) 
) 

하지만 내가 정말 필요로하는 것은이 같은 바인드 쿼리 :

select * from "order_items" where "order_items"."name" = 'test' and "order_items"."deleted_at" is null 

나는 원시 PHP로 이것을 할 수 있지만 laravel core에는 어떤 해결책이 있는가? 당신이 할 수있는

답변

0

그와 함께 :

 

    OrderItem::where('name', '=', 'test')->toSql(); 

+0

코드 출력이 없습니다. 바인드 쿼리가 아닙니다. -> select * from "order_items"여기서 "order_items". "name"=? 및 "order_items". "deleted_at"이 (가) null입니다. – fico7489

0

그래, 네 말이 맞아 :/ 이 매우 요구 된 기능이며, 내가 왜 프레임 워크의 일부가 아닌 아직 아무 생각이 ...

이 가장 우아한 해결책은 아니지만이 같은 수행 할 수 있습니다 동일한에 대한 helpers.php

 


    function getPureSql($sql, $binds) { 
     $result = ""; 

     $sql_chunks = explode('?', $sql); 
     foreach ($sql_chunks as $key => $sql_chunk) { 
      if (isset($binds[$key])) { 
       $result .= $sql_chunk . '"' . $binds[$key] . '"'; 
      } 
     } 

     return $result; 
    } 

    $query = OrderItem::where('name', '=', 'test'); 
    $pure_sql_query = getPureSql($query->toSql(), $query->getBindings()); 

    // Or like this: 
    $data = OrderItem::where('name', '=', 'test')->get(); 

    $log = DB::getQueryLog(); 
    $log = end($log); 
    $pure_sql_query = getPureSql($log['query'], $log['bindings']); 
 
1

사실은 내가 만든 하나 개의 함수를. 또한

if (! function_exists('ql')) 
{ 
    /** 
    * Get Query Log 
    * 
    * @return array of queries 
    */ 
    function ql() 
    { 
     $log = \DB::getQueryLog(); 

     $pdo = \DB::connection()->getPdo(); 

     foreach ($log as &$l) 
     { 
      $bindings = $l['bindings']; 

      if (!empty($bindings)) 
      { 
       foreach ($bindings as $key => $binding) 
       { 
        // This regex matches placeholders only, not the question marks, 
        // nested in quotes, while we iterate through the bindings 
        // and substitute placeholders by suitable values. 
        $regex = is_numeric($key) 
         ? "/\?(?=(?:[^'\\\']*'[^'\\\']*')*[^'\\\']*$)/" 
         : "/:{$key}(?=(?:[^'\\\']*'[^'\\\']*')*[^'\\\']*$)/"; 

        $l['query'] = preg_replace($regex, $pdo->quote($binding), $l['query'], 1); 
       } 
      } 
     } 

     return $log; 
    } 
} 

if (! function_exists('qldd')) 
{ 
    /** 
    * Get Query Log then Dump and Die 
    * 
    * @return array of queries 
    */ 
    function qldd() 
    { 
     dd(ql()); 
    } 
} 

if (! function_exists('qld')) 
{ 
    /** 
    * Get Query Log then Dump 
    * 
    * @return array of queries 
    */ 
    function qld() 
    { 
     dump(ql()); 
    } 
} 

단순히 helpers.php 파일 내에서이 세 가지 기능을 배치하여 helpers.php 파일 내에서 동일한 기능을 사용할 수 있으며 다음과 같이 동일 사용할 수 있습니다

$items = OrderItem::where('name', '=', 'test')->get(); 
qldd(); //for dump and die 

을하거나

qld(); // for dump only 
을 사용할 수 있습니다