2013-08-17 2 views
5

이렇게 laravel 쿼리 작성기를 사용하고 있습니다.Laravel 쿼리 작성기가 값을 바인딩하지 않습니다.

$col1 = Input::get('col1',''); 
$col2 = Input::get('col2',''); 
$result = DB::table('table1') 
     ->select('id', 'col1', 'col2', 'col3') 
     ->whereRaw("col1 like '%?%'", [$col1]) 
     ->whereRaw("col2 like '%?%'", [$col2]) 
     ->orderBy($orderBy, $orderType) //orderBy=col1, ordeyType=ASC 
     ->skip($ofset)->take($limit) //$ofser=0, $limit=10 
     ->get(); 

나는 아무것도 얻지 못합니다. 만약 내가 toSql() 함수를 사용합니다. 나는이 SQL을 이런 식으로 얻고있다.

select `id`, `col1`, `col2`, `col3` 
from `table1` where col1 like '%?%' and col2 like '%?%' 
order by `col1` ASC limit 10 offset 0 

물음표는 거기에 있으면 안된다. 값으로 대체해야합니다. 이 코드를 사용하여 디버깅했습니다.

Log::info(var_export(DB::getQueryLog(), true)); 

로그는 내가 바인딩 내가 뭔가를 잘못하고 있어요 홍보를 작동하지 않는 생각이

2 => 
array (
'query' => 'select `id`, `col1`, `col2`, `col3` from `table1` where col1 like \'%?%\' and col2 like \'%?%\' order by `col1` ASC limit 10 offset 0', 
'bindings' => 
    array (
     0 => 'k', 
     1 => '', 
    ), 
'time' => 25.71, 

처럼 보인다. 왜냐하면 내가 데이터베이스에서이 코드를 시도하면 작동한다.

select `id`, `col1`, `col2`, `col3` from `table1` 
where col1 like '%k%' and col2 like '%%' 
order by `col1` ASC limit 10 offset 0 

답변

0

->whereRaw("col1 like '%?%'", [$col1]) 
    ->whereRaw("col2 like '%?%'", [$col2]) 

->whereRaw("col1 like '%?%'", $col1) 
    ->whereRaw("col2 like '%?%'", $col2) 
10

그것을 파악하려고 (추가, 난. 내가 그걸 어떻게해야합니까? 데이터베이스에 보내 실제 SQL을 얻고 싶은) . ? 자체적으로 이동해야하므로 % 기호를 사용자의 col 변수에 연결하십시오. 그리고

변경 (당신이 Laravel 4를 사용하는 가정) 배열에 COL 변수를 넣어 :

->whereRaw("col1 like '%?%'", [$col1]) 
->whereRaw("col2 like '%?%'", [$col2]) 

에 :

->whereRaw("col1 like ?", array('%'.$col1.'%')) 
->whereRaw("col2 like ?", array('%'.$col2.'%')) 
+0

감사가 – muhammedea

+1

을 workd 내가이 더 많은 찬성 투표 수 있으면 좋겠다. 주변에 많은 잘못된 예가 있는데, 나는 손목을 자르려고하고 있었다. – Toskan

관련 문제