我想创建使用数据库搜索数据的服务API。
这是我当前的查询:
public function show($fructose,$lactose,$polyols,$fructan )
{
$FodMaps = FodMap::where('fructose','=',$fructose)->
where('lactose','=',$lactose)->
where('polyols','=',$polyols)->
where('fructan','=',$fructan)->
get();
return $FodMaps;
}我的服务URL是:
/api/service/lactose=high/fructose=high/polyols=high/fructan=high它应该包含高和低的as值。但有时它的值是"none“,比如:
/api/service/lactose=high/fructose=high/polyols=none/fructan=none我想要的是,如果它包含任何"none“值,则从查询中转义该项目并执行搜索。因为当前查询搜索所有4个值。
例如:如果URL的值如下所示:
/api/service/lactose=high/fructose=high/polyols=high/fructan=high它将检查匹配所有4个项目的结果。
但如果是这样的话:
/api/service/lactose=high/fructose=high/polyols=none/fructan=none它将只搜索乳糖,果糖和多聚糖柱,并给出结果,不需要检查果糖柱的值。
发布于 2015-08-01 02:52:13
使用函数检查变量
$FodMaps = FodMap::where(function($query) use($fructose,$lactose,$polyols,$fructan){
if($fructose != 'none'){
$query->where('fructose','=',$fructose);
}
if($lactose != 'none'){
$query->where('lactose','=',$lactose);
}
if($polyols != 'none'){
$query->where('polyols','=',$polyols);
}
if($fructan != 'none'){
$query->where('fructan','=',$fructan);
}
})
->get();https://stackoverflow.com/questions/31752700
复制相似问题