在我的laravel(7.x)应用程序中,我具有一个通用功能来显示所有模块中所有活动和非活动记录的计数。因此,我有义务在每个模块上重复相同的功能。
例如:Device, DeviceType, DeviceCompany, etc
模型具有称为_getTotal
的相同方法,并且在_getTotal
方法执行相同工作的所有地方。
Device.php
class Device extends Model
{
protected $table = 'devices';
...
public function _getTotal($status = \Common::STATUS_ACTIVE)
{
return self::where([
'status' => $status
])->count() ?? 0;
}
}
DeviceType.php
class DeviceType extends Model
{
protected $table = 'device_types';
...
public function _getTotal($status = \Common::STATUS_ACTIVE)
{
return self::where([
'status' => $status
])->count() ?? 0;
}
}
我试图将此方法放入Base Model
,但我认为可能不是一个好习惯。我对吗..?有什么方法可以使此方法_getTotal
成为所有模块的通用方法。
在我的laravel(7.x)应用程序中,我具有一个通用功能来显示所有模块中所有活动和非活动记录的计数。因此,我有义务在...
您可以将此方法移至特征,然后将特征包括在内,而不是所有需要此方法的类。