【发布时间】:2014-07-03 09:26:00
【问题描述】:
我想知道如何将以下用户模型雄辩的业务逻辑重写为更干一点。我将三个参数传递给模型,其中所有参数都是可选的,以缩小数据库搜索范围。我确信必须有一种优雅的方式来做到这一点,但目前它超出了我的能力范围。
public function guests_age($location_id, $year, $gender)
{
if($location_id && $year && $gender)
{
return Guest::select('age')
->where('location_id', '=', $location)
->where('created_at', '=', $year)
->where('gender', '=', $gender)
->get();
}
elseif($location_id && $year && !$gender)
{
return Guest::select('age')
->where('location_id', '=', $location)
->where('created_at', '=', $year)
->get();
}
elseif($location_id && !$year && !$gender)
{
return Guest::select('age')
->where('location_id', '=', $location)
->get();
}
... and so on to cover all cases...
}
感谢您的帮助。
【问题讨论】:
标签: php laravel model eloquent dry