Php Laravel中数据库的自定义查询
首先,我想知道这在拉威尔是否可能 我有以下代码:Php Laravel中数据库的自定义查询,php,laravel,Php,Laravel,首先,我想知道这在拉威尔是否可能 我有以下代码: $master_array = $_POST['master_search_array']; $count = count($master_array); $master_string = ''; for($i=0; $i<$count; $i++) { if($master_array[$i] == "Dining"){ $master_string .= "where('dining', 'dining')"
$master_array = $_POST['master_search_array'];
$count = count($master_array);
$master_string = '';
for($i=0; $i<$count; $i++) {
if($master_array[$i] == "Dining"){
$master_string .= "where('dining', 'dining')";
}
if($master_array[$i] == "Party"){
$master_string .= "where('party','party')";
}
....ETC you get the point
}
$tours = DB::table('tours')->$master_string->get();
return $tours;
如何在laravel中实现这一点,它给了我一个错误,无论我将其作为
$master_string
或{{$master_string}}
传递,都不需要master string。只需使用查询生成器就可以了
$master_array = $_POST['master_search_array'];
$count = count($master_array);
$query = DB::table('tours');
for($i=0; $i<$count; $i++) {
if($master_array[$i] == "Dining"){
$query->where('dining', 'dining');
}
if($master_array[$i] == "Party"){
$query->where('party', 'party');
}
}
$tours = $query->get();
return $tours;
$master\u array=$\u POST['master\u search\u array'];
$count=count($master\u数组);
$query=DB::table('tours');
对于($i=0;$iwhere('dinning','dinning');
}
if($master_数组[$i]=“参与方”){
$query->where('party','party');
}
}
$tours=$query->get();
回程$旅游;;
使用在查询过程中添加位置的功能
DB::table('tour')->where(function($query) use ($master_array)
{
foreach($master_array as $k => $v) {
if($v == "Dining"){
$query->where('dining','dining');
}
if($v == "Party"){
$query->where('party','party');
}
}
})
->get();
我甚至没有想到这一点,我总是把事情复杂化^^谢谢
DB::table('tour')->where(function($query) use ($master_array)
{
foreach($master_array as $k => $v) {
if($v == "Dining"){
$query->where('dining','dining');
}
if($v == "Party"){
$query->where('party','party');
}
}
})
->get();