如何从PHP Laravel中的两个日期之间从数据库获取所有数据? [英] how to get all data from db between two dates in PHP Laravel?

查看:437
本文介绍了如何从PHP Laravel中的两个日期之间从数据库获取所有数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法接收在两个日期之间插入的所有数据.例如开始日期:2017-01-15-2018-05-15:结束日期.

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

当我dd($ids);时,它仅显示正在为该项目工作的1位用户,而不是全部. (目前有两个工作).

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

UserWages的可填充内容如下:

fillables for UserWages are these:

 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',
    ];

所有这一切的目的是,我试图获得在项目开始和结束期间为此项目工作的每个用户的工资. 工资可能每个月都在变化,因此它们的汇率(可以用美元,欧元或阿尔巴尼亚列克付款);

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.

汇率代码是这样的:

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

可填充的汇率为

protected $fillable =[

    'euro_lek', 'dollar_lek', 'date',
];

推荐答案

我认为我发现了问题所在. 您应该使用 whereIn 来获取所有用户工资.

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

更改此:

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

这篇关于如何从PHP Laravel中的两个日期之间从数据库获取所有数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆