2016-12-16 53 views
0

我有一個示例2表。一個是學者與列出生日期獎學金表。我在這裏創建的是通過獲得年齡範圍的標準。使用條件Age_fromAge_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

你可以試試與

//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();   

}