0

我有两张表,employees table现在payrolls table我正在尝试将数据添加payrolls table到计算员工的总金额中,但是当我尝试这样做时,我不断收到该错误,有人可以帮我解决这个问题!

这里是 Payroll model`

protected $dates = ['deleted_at'];

protected $fillable = ['over_time', 'notified','hours','rate', 'gross', 'employee_id'];

public function employee(){
    return $this->belongsTo('App\Employee');
}

public function grossPay(){
    $calc = 0;
    if($this->employee->full_time && !$this->over_time){
        return $this->gross = $this->employee->role->salary;
    }
    if($this->employee->full_time && $this->over_time){
        $calc = $this->hours * $this->rate;
        return $this->gross = $calc + $this->employee->role->salary;
    }
    if($this->over_time || !$this->employee->full_time){
        $calc = $this->hours * $this->rate;
        return $this->gross = $calc;
    }
    return $this->$gross = 0;
}

}`

这是路线: Route::post('/payrolls/{id}', 'PayrollController@store')->name('payrolls.store');

工资控制器

 public function store(Request $request)
{
    $this->validate($request ,[
        'over_time' => 'required|bool',
        'hours' => 'required',
        'rate' => 'required'
    ]);

    $payroll = new Payroll;
    $payroll->over_time = $request->over_time;
    $payroll->hours = $request->hours;
    $payroll->rate = $request->rate;
    $payroll->employee_id = $request->employee_id;
    $payroll->grossPay();
    $payroll->save();

    return redirect('payroll.show', ['employee_id'=>$employee_id] )->with('success','payroll created');

}
4

1 回答 1

0

您应该找到一名员工,然后检查适当的条件。

public function grossPay(){
    $calc = 0;
    $employee = Employee::findOrFail($this->employee_id);

    if($employee->full_time && !$this->over_time){
        return $this->gross = $this->employee->role->salary;
    }
    if($employee->full_time && $this->over_time){
        $calc = $this->hours * $this->rate;
        return $this->gross = $calc + $employee->role->salary;
    }
    if($this->over_time || !$this->full_time){
        $calc = $this->hours * $this->rate;
        return $this->gross = $calc;
    }
    return $this->gross = 0;
  }
于 2020-03-11T07:51:24.293 回答