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



我有一个人员控制器和事务模型。在人员控制器中,我正在获取具有以下其他数据的人员事务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 IlluminateDatabaseEloquentBuilder::earnedByStaff() in file

我已经尝试使用$transactions = Transaction::earnedByStaff($staff)...,但这返回

Error: Non-static method AppTransaction::earnedByStaff() cannot be called statically in file 

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

设置函数' eardbystaff '为静态

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

之后就可以正常工作了。

可以使用作用域方法

public function scopeEarnedByStaff($query,$arg)
{
.....
}

您还可以使用查询构建器上的call方法来调用模型类上的函数

MyModel::where('column', 'value')->get();
$transactions = Transaction::thisMerchant()
->call('earnedByStaff', [$staff])->where()
..
..

相关内容

  • 没有找到相关文章

最新更新