laravel 如何调用带参数的Eloquent函数?

ix0qys7i  于 2023-01-14  发布在  其他
关注(0)|答案(1)|浏览(119)

我有一个员工控制器和事务模型。在员工控制器中,我正在获取员工事务和其他数据,如下所示StaffController.php

$transactions = Transaction::thisMerchant()
->earnedByStaff($staff)
  ->where(function ($q) use ($staffID) {
    $q->where('staff_id', $staffID)
      ->orWhere(function ($qq) use ($staffID) {
        $qq
          ->whereNull('staff_id')
          ->whereHas('items', function ($qqq) use ($staffID) {
            $qqq->where('staff_id', $staffID);
          });
      })
      ->orWhereHas('associateCommissions', function ($qq) use ($staffID) {
        $qq->where('staff_id', $staffID);
      });
  })
  ->when($start_date && $end_date, function ($q) use (
    $start_date,
    $end_date
  ) {
    $q->whereBetween(\DB::raw('date(date)'), [
      $start_date . ' 00:00:00',
      $end_date . ' 23:59:59',
    ]);
  })
  ->with('client')
  ->orderBy('date', 'DESC')
  ->where('is_void', null)
  ->where('is_draft', null)
  ->with('items.sellable.sellable')
  ->with('associateCommissions')
  ->paginate(10);

在事务模型中,我有这个Transaction.php

public function earnedFromTransaction($staff_id)
{
$staff = Staff::find($staff_id);
if ($this->is_void == '1') {
  return 0;
}

if ($this->provider() == 'Empty') {
  return 0;
}

if ($this->provider() == 'Split') {
  $amounts = Titem::where('transaction_id', $this->id)
    ->where('staff_id', $staff_id)
    ->whereHas('product', function ($q) {
      $q->whereNull('commission');
    })
    ->whereHas('transaction', function ($q) {
      $q->where('is_void', null);
    })
    ->sum('frozen_staff_commission');

  $discounts = Titem::where('transaction_id', $this->id)
    ->where('staff_id', $staff_id)
    ->whereHas('product', function ($q) {
      $q->whereNull('commission');
    })
    ->whereHas('transaction', function ($q) {
      $q->where('is_void', null);
    })
    ->sum('discount');

  // When the frozen staff rate is zero, just give sellable commission
  $titem_commissions = Titem::where('transaction_id', $this->id)
    ->where('staff_id', $staff_id)
    ->where('frozen_staff_commission', '<=', 0)
    ->whereHas('product', function ($q) {
      $q->whereNotNull('commission');
    })
    ->sum('commission');

  $total =
    floatval($amounts) +
    floatval($titem_commissions) -
    floatval($discounts);

  return $total;
}

return floatval($this->frozen_staff_commission);
  }

  public function earnedByStaff(Staff $staff)
  {
   return StaffCommissionEngine::earnedFromTransaction($this, $staff);
  }

我收到此错误BadMethodCallException: Call to undefined method Illuminate\Database\Eloquent\Builder::earnedByStaff() in file
我已经尝试使用$transactions = Transaction::earnedByStaff($staff)...,但返回

Error: Non-static method App\Transaction::earnedByStaff() cannot be called statically in file

如何调用earnedByStaff()方法并将$staff传递给它?

tgabmvqs

tgabmvqs1#

将函数“earnedByStaff”设置为静态。

public static function earnedByStaff( Staff $staff)
{
   return StaffCommissionEngine::earnedFromTransaction($this, $staff);
}

在那之后它就会工作得很好。

相关问题