[英]Laravel 5 Service API queries with MySQL
我想为使用数据库的搜索数据创建Service 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;
}
我的服务网址是:
/api/service/lactose=high/fructose=high/polyols=high/fructan=high
它应包含较高和较低的值。 但有时它带有“无”值,例如:
/api/service/lactose=high/fructose=high/polyols=none/fructan=none
我想要的是如果它包含任何“无”值,请从查询中将该项目转义并进行搜索。 因为当前查询搜索所有4个值。
例如:如果URL带有以下值:
/api/service/lactose=high/fructose=high/polyols=high/fructan=high
它将检查与所有4个项目匹配的结果。
但是如果附带了这个:
/api/service/lactose=high/fructose=high/polyols=none/fructan=none
它将仅搜索乳糖,果糖和多元醇的列,并给出结果,而无需检查果聚糖柱的值。
使用功能检查变量
$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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.