1

I am failing to receive all data which are inserted between two dates. for example start date: 2017-01-15 - 2018-05-15 : end date.

I want to get all wages of people who have worked for a specific project during that period of time, and my code is this so far:

$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;
    }
}

when I dd($ids); it shows only 1 user that's working for the project, not all of them. (currently two working on it).

fillables for UserWages are these:

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

, Projects:

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

, User:

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

The purpose of all this is that I am trying to get the wage of every user who has worked for this project during the of project start and project end. Wages might change from month to month, and so the exchange rate of them (payment can be done in USD, Euro or Albanian Lek);

I am trying to get all wages of users who are part of the project, and calculate data, for each pay that they might have had. with the exchange rate of that month, exchange rate can be filled by the admin.

code for Exchange rate is this:

    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 for Exchange rate are

protected $fillable =[

    'euro_lek', 'dollar_lek', 'date',
];
7
  • Why you have return in the second foreach loop? It will only return one record. Query is ok. You have to elaborate your question. Commented Sep 5, 2017 at 7:50
  • @forexknight hey, I updated the question, can you check again? Commented Sep 5, 2017 at 7:59
  • When you do dd($ids) the loop halts at the first iteration. That's why it show only one user. use dump($ids) instead. Commented Sep 5, 2017 at 8:01
  • You should show us the deepth of this: $usersOfProject = $project->users()->get(); Commented Sep 5, 2017 at 8:04
  • @Gayan to check how many users are, but yes I got your point, it was a mistake I didnt thought, but when I dd($userWage->value) I get nothing Commented Sep 5, 2017 at 8:05

2 Answers 2

1

I think that I've found what's wrong. You should use whereIn to get all user wages.

Change this:

$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;
}

}

Into this:

$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;
Sign up to request clarification or add additional context in comments.

9 Comments

htmlspecialchars() expects parameter 1 to be string, array given it says that its an error in helpers line 547 when I only have 540 lines in helpers
Yes, actually we should get only id of the user and then convert it to array.
I forgot to add $wage=$wage + $userWage->value; I had it `$wage=wage + $userWage->value;' anyway, when it shouldnt be there at all. because it sums all wages.
so it works, but should I create a function to convert it all for the last exchange rate of the month? or is it possible to be done inside this function?
It's better to create new function. Also if the answer is corrent you should check it as corrent answer, to show other users that the case has been solved.
|
0

I would make it in a different way:

    $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);

1 Comment

get an empty array, but I thought that it might be the problem in date, so I checked out. if I enter manually dates at $userWage it gets data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.