试图获取非对象PHP Laravel的属性

时间:2017-09-08 14:52:47

标签: php laravel php-carbon

我知道tittle中的这个问题在这里被问得过多了,我通过了大部分内容但仍无法找到我的代码的解决方案。

function calculatingWages($project_id){
    $start_date = '2017-05-01';
    $end_date = '2017-12-31';

    $project = Project::find($project_id);
    $users = $project->users()->get();

    $sumWage=0;
    foreach ($users as $user){
        $timesheetHours = $user->timesheets()->whereBetween('timesheets.date',[$start_date,$end_date])->sum('hours');
        $wages = UserWage::whereBetween('start_date',[ $start_date,$end_date])->whereBetween('end_date',[ $start_date,$end_date])->get();

        foreach ($wages as $wage){

            $value = $wage->value;
            $currency = $wage->currency;
            $sumWage =   extractMonthsAndCalculate($value,$currency, $timesheetHours, $start_date, $end_date);
        }
        return $sumWage;
    }
}

function extractMonthsAndCalculate($value,$currency, $timesheetHours, $start_date, $end_date){
    $start = Carbon::createFromFormat('Y-m-d',$start_date)->month;
    $end = Carbon::createFromFormat('Y-m-d',$end_date)->month;

    $diffOfMonths = $end - $start;
    $sumWage = 0;
    for ($i = $start; $i <= $diffOfMonths; $i++) {
        $wageYear  = Carbon::createFromFormat('Y-m-d',$start_date)->year;

        $wageDay = Carbon::createFromDate($wageYear,$i,'01')->lastOfMonth()->toDateString();
       $test =  convertingALL($value,$currency,$timesheetHours,$wageDay);
    }
    return $sumWage;
}

function convertingALL($value, $currency, $timesheetHours, $date)
{
    $currencyObj = Exchange::where('date',$date)->get()->first();
    $currencyDate = $currencyObj->date;

     $hourlyWage = 0;

    $sumWage = 0;
    if($currencyDate == $date) {

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

        if ($currency == 'ALL') {
            $sumWage = $value;
        } elseif ($currency == 'USD') {
            $sumWage = ($hourlyWage *$timesheetHours) * $dollar;
        } else {
            $sumWage = ($hourlyWage *$timesheetHours)* $euro;
        }
   }else{
        $euro = 140;
        $dollar = 136.4;

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

它说它无法在第468行获得非对象的属性

这是第467-468行:

$currencyObj = Exchange::where('date',$date)->get()->first();
    $currencyDate = $currencyObj->date;

当我dd $ currencyDate打印它的日期时,试图用碳解析它但仍然是同样的东西,我在哪里弄乱?

1 个答案:

答案 0 :(得分:1)

你需要告诉Eloquent date字段包含一个日期(即使它看起来很明显)。

文档:https://laravel.com/docs/5.4/eloquent-mutators#date-mutators

在您的Exchange模型中,您应该

class Exchange extends Model {

    protected $dates = [ 'date' ];

在一个不相关的注释中,->get()->first()将从数据库中取回每一个结果,然后将其中一个除去。如果您只是致电->first(),那么您只会从数据库中获得一个结果;相同的最终结果,但更好的性能。