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 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)