Laravel 4: query builder advanced where
-
21-12-2019 - |
Question
I am trying to do a function for auto generating query with advanced where for search function.
Here is my example code:
$query->where(function($thisquery){
$datacolumn = DB::select('SHOW COLUMNS FROM '.mysql_real_escape_string($tablename));
for($i=0;$i<sizeof($datacolumn);$i++){
$field = $datacolumn[$i]->Field;
$thisquery->orWhere($field,'like','%'.$keyword.'%');
}
});
The problem is how can I pass variables into the function?
Solution
Use the use keyword
$query->where(function($thisquery) use ($yourVariable, $yourVariable2) {
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow