1

My function in controller for get data when user search ajax.

This is my code:

$receiptNum = Request::get('receiptNum');
// echo $receiptNum = 123456
DB::connection()->enableQueryLog();
$q = DB::table('tbl_receipt AS r')
            ->join('company AS com', 'r.com_id', '=', 'com.com_id')
            ->join('branches AS b', 'com.b_id', '=', 'b.b_id')
            ->join('employee AS e', 'r.created_by', '=', 'e.e_id')
            ->where('r.receipt_code','=',$receiptNum)
            ->get();
$query = DB::getQueryLog();
var_dump($query);

I don't know what missing in my code, it's show raw sql like this:

SELECT *
FROM       "tbl_receipt" AS "r"
INNER JOIN "company"     AS "com" ON "r"."com_id" = "com"."com_id"
INNER JOIN "branches"    AS "b"   ON "com"."b_id" = "b"."b_id"
INNER JOIN "employee"    AS "e"   ON "r"."created_by" = "r"."e_id"
WHERE "r"."receipt_code" = ?

I trying to replace $receiptNum by num 123 for test, it's also show ? in raw sql, please help me to solve it.

lorond
  • 3,856
  • 2
  • 37
  • 52
ching
  • 122
  • 7

1 Answers1

1

The "?" there are because it is a Statement. If you want a string you have to replace.

I was inspired by https://gist.github.com/JesseObrien/7418983

and for example I make this.

    $q = DB::table('tbl_receipt AS r') 
->join('company AS com', 'r.com_id', '=', 'com.com_id') 
->join('branches AS b', 'com.b_id', '=', 'b.b_id') 
->join('employee AS e', 'r.created_by', '=', 'e.e_id') 
->where('r.receipt_code','=','123') ; 

$sql = $q->toSql(); 
// $bindings = $q->getBindings(); 
foreach($q->getBindings() as $binding) 
{ 
$value = is_numeric($binding) ? $binding : "'".$binding."'"; 
$sql = preg_replace('/\?/', $value, $sql, 1); 
} 
var_dump($sql);
LorenzoBerti
  • 6,704
  • 8
  • 47
  • 89