doxqszx09742 2017-09-08 14:52
浏览 61
已采纳

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

I know that this question in the tittle is asked WAYY too much in here, and I went thru most of them but still cant find a solution for my code.

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

it says that it cant get the property of a non object in line 468

this is line 467-468:

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

when I dd $currencyDate it prints the date of it, tried to parse it using carbon but still same thing, where am I messing up?

  • 写回答

1条回答 默认 最新

  • douzhang1364 2017-09-08 15:10
    关注

    You need to tell Eloquent that the date field contains a date (even though it seems obvious).

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


    In your Exchange model you should have

    class Exchange extends Model {
    
        protected $dates = [ 'date' ];
    

    On an unrelated note, ->get()->first() will pull every single result back from the database, then chuck all but one of them away. If you just call ->first() then you'll only get one result from the database; same end result but better for performance.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化