我有一个样本2表。一个是学者,列出生日期和奖学金表。我在这里创造的是通过获得年龄范围的标准。使用条件 Age_from 和 Age_to 。我想得到/显示在该范围内找到的行。这段代码的错误可能是什么,因为返回总是空的。
非常感谢任何帮助。
数据库学者
scholar_id Date_of_birth Ship_level
1 1991-12-19 Tertiary
2 1990-01-19 Secondary
3 2008-03-19 Primary
4 1992-21-19 Tertiary
5 1991-12-19 Tertiary
6 2000-10-12 Secondary
7 2001-12-23 Secondary
8 2009-12-19 Primary
数据库奖学金
ship_id Age_from Age_to Level
1 10 16 Primary
我的代码
public function ListOrgaScholar($ship_id)
{
$ship = Scholarship::find($ship_id);
$Agefrom = $ship->Age_from;
$Ageto = $ship->Age_to;
$Level = $ship->Level;
$scholars = (new Scholar)->newQuery()->select('*');
if($Level)
$scholars->where('Level', '=', $Level);
$birthDates = $scholars->selectRaw('Date_of_birth');
$ag = [];
foreach($birthDates as $birthDatee){
list($m,$d,$y)=explode('-',$birthDatee->Date_of_birth);
$age = Carbon::createFromDate($m,$d,$y)->age;
array_push($ag,$age);
}
$scholars = $scholars->get();
}
答案 0 :(得分:0)
您可以尝试使用
//use Carbon\Carbon; - import Carbon in your class/controller
public function ListOrgaScholar($ship_id)
{
$ship = Scholarship::find($ship_id);
$fromDate = Carbon::now()->subYears($ship->Age_from)->format('Y-m-d');
$endDate = Carbon::now()->subYears($ship->Age_to)->format('Y-m-d');
$scholars = Scholar::where('Ship_level', $ship->Level)
->whereBetween('Date_of_birth', [$fromDate, $toDate])
->get();
}