2017-09-05 68 views
1

我無法接收在兩個日期之間插入的所有數據。例如開始日期:2017-01-15 - 2018-05-15:結束日期。如何在PHP Laravel的兩個日期之間從db獲取所有數據?

我想是誰在那段時間工作了一個具體項目的人都工資,我的代碼是這樣的,到目前爲止:

$project = Project::find($project_id); 
$projectStartDate = $project->start_date; 
$projectEndDate = $project->end_date; 

$usersOfProject = $project->users()->get(); 

foreach ($usersOfProject as $userOfProject){ 

    $ids = $userOfProject->id; 

    $userWages = UserWage::where('user_id',$ids)->where('start_date','<=', $projectStartDate)->where('end_date','>=',$projectEndDate)->get(); 
    foreach ($userWages as $userWage){ 
     $wage= wage + $userWage->value; 

     return $wage; 
    } 
} 

當我dd($ids);,僅顯示1中的工作用戶對於這個項目,不是全部。 (目前有兩個工作)。

fillables爲UserWages是這些:

protected $fillable = [ 
     'value', 'currency', 'user_id', 'start_date', 'end_date', 
    ]; 

,項目:

protected $fillable = [ 
     'name', 'description', 'start_date', 'end_date', 'value', 'client_id','currency','', 
    ]; 

,用戶:

protected $fillable = [ 
     'email', 'password', 'first_name', 'last_name', 'status_id','mobile', 'role', 
    ]; 

所有這一切的目的是,我試圖讓在項目開始和項目結束期間爲該項目工作的每個用戶的工資。 工資可能會逐月改變,所以他們的匯率(付款可以美元,歐元或阿爾巴尼亞列克);

我正在嘗試獲取屬於該項目的用戶的所有工資,並計算他們可能擁有的每項工資的數據。以當月的匯率計算,匯率可由管理員填寫。匯率

代碼是這樣的:

function exchangeRate($value, $currency){ 
$getCurrency = Exchange::whereDate('date','2017-11-30'); //This is just for test to calculate wages with exchange rate of november 30, will do with carbon to get last date of month. 

     $dollar = $getCurrency->dollar_lek; 
     $euro = $getCurrency->euro_lek; 

     if ($currency == 'ALL') { 
      $value = $value; 
     } elseif ($currency == 'USD') { 
      $value = $value * $dollar; 
     } else { 
      $value = $value * $euro; 
     } 
    } 

fillables匯率是

protected $fillable =[ 

    'euro_lek', 'dollar_lek', 'date', 
]; 
+0

爲什麼你必須在返回第二個foreach循環?它只會返回一條記錄。查詢是可以的。你必須詳細說明你的問題。 –

+0

@forexknight嘿,我更新了這個問題,你能再次檢查嗎? – BTEK

+0

當你執行'dd($ ids)'時,循環在第一次迭代時暫停。這就是爲什麼它只顯示一個用戶。改用'dump($ ids)'。 – Gayan

回答

1

我想我已經找到了什麼問題。 您應該使用其中在獲得所有用戶工資。

更改此:

$project = Project::find($project_id); 
$projectStartDate = $project->start_date; 
$projectEndDate = $project->end_date; 

$usersOfProject = $project->users()->get(); 

foreach ($usersOfProject as $userOfProject){ 

    $ids = $userOfProject->id; 

$userWages = UserWage::where('user_id',$ids)->where('start_date','<=', $projectStartDate)->where('end_date','>=',$projectEndDate)->get(); 
foreach ($userWages as $userWage){ 
    $wage= wage + $userWage->value; 
    //it should be pushed into array and not returning single value. 
    return $wage; 
} 

}

進入這個:

$project = Project::find($project_id); 
$projectStartDate = $project->start_date; 
$projectEndDate = $project->end_date; 
$wages = array(); 

$usersOfProject = $project->users()->get(); 

foreach ($usersOfProject as $userOfProject){ 

    $userWages = UserWage::whereIn('user_id',$usersOfProject>pluck('id')->toArray())->where('start_date','<=', $projectStartDate)->where('end_date','>=',$projectEndDate)->get(); 
    foreach ($userWages as $userWage){ 
     $wage= wage + $userWage->value; 
     array_push($wages, $wage); 
    } 
} 

return $wages; 
+0

'htmlspecialchars()期望參數1是字符串,給定的數組' 它說,它在幫助者行547中的錯誤,當我只有幫助者540行 – BTEK

+0

是的,實際上,我們應該只獲得用戶的id,然後轉換它來陣列。 –

+0

我忘了添加'$工資= $工資+ $ userWage->價值;'我有'$工資=工資+ $ userWage->價值;'無論如何,當它不應該在那裏。 因爲它總計所有工資。 – BTEK

0

我會讓它以不同的方式:

$project = Project::find($project_id); 
    $projectStartDate = Carbon::parse($project->start_date)->startOfDay(); 
    $projectEndDate = Carbon::parse($project->end_date)->endOfDay(); 

    $usersOfProject = $project->users()->get(); 


    $userWages = UserWage::whereIn('user_id',$usersOfProject->pluck('id')->toArray())->whereBetween('start_date',[ $projectStartDate,$projectEndDate])->whereBetween('end_date',[ $projectStartDate,$projectEndDate])->get(); 

    $wages = []; 
    foreach ($userWages as $userWage){ 
     $wage= wage + $userWage->value; 
     array_push($wages,$wage); 
    } 

    dd($userWages,$wages); 
+0

得到一個空數組,但我認爲這可能是日期問題,所以我檢查了出來。 如果我在'$ userWage'手動輸入日期,它會得到數據 – BTEK

+0

嘗試更新答案 – aaron0207

相關問題