Php 在Laravel中防止模型中的重复代码
我有两个不同的模型(ErrorLog和EntityLog)和一些类似的代码,我怎样才能防止呢? EntityLog具有以下代码:Php 在Laravel中防止模型中的重复代码,php,laravel,Php,Laravel,我有两个不同的模型(ErrorLog和EntityLog)和一些类似的代码,我怎样才能防止呢? EntityLog具有以下代码: class EntityLog extends Model { protected const ACTION = 'action'; /** * The attributes that are mass assignable. * * @var array */ protected $fillable
class EntityLog extends Model
{
protected const ACTION = 'action';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'entity', 'type', 'icon', 'action', 'action_detail', 'action_user_id'
];
protected $with = ['action_user'];
public function action_user()
{
return $this->belongsTo('App\User', Config::get('constants.LOGGING.ACTION_USER_ID'));
}
/**
* Create Entity log
*
* @param $type
* @param array $info
* @return mixed
*/
public static function add($type, $info)
{
$entity = $info['entity'];
$entity_id = $info['entity_id'] ?? '';
$action_user_id = $info['action_user_id'] ?? auth()->user()->id ?? Config::get('constants.SYSTEM_ADMIN');
$action_detail = $info['action_detail'] ?? '';
switch ($type){
case 'create':
$icon = 'fa-plus bg-purple';
$action = $info[self::ACTION] ?? 'created successfully';
break;
case 'update':
$icon = 'fa-pencil bg-teal';
$action = $info[self::ACTION] ?? 'updated successfully';
break;
case 'delete':
$icon = 'fa-trash bg-red';
$action = $info[self::ACTION] ?? 'deleted successfully';
break;
default:
$icon = $action = 'invalid_flag';
}
Log::info($type.' : '.$entity.' : '.$entity_id.' : '.$action.' : '.$action_detail.' : '.$action_user_id);
return self::create([
Config::get('constants.LOGGING.ENTITY') => $entity,
Config::get('constants.LOGGING.ENTITY_ID') => $entity_id,
Config::get('constants.LOGGING.TYPE') => $type,
Config::get('constants.LOGGING.ICON') => $icon,
Config::get('constants.LOGGING.ACTION') => $action,
Config::get('constants.LOGGING.ACTION_DETAIL') => $action_detail,
Config::get('constants.LOGGING.ACTION_USER_ID') => $action_user_id,
]);
}
}
类错误日志有以下代码
class ErrorLog extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'entity', 'type', 'icon', 'action', 'action_detail', 'action_user_id'
];
protected $with = ['action_user'];
public function action_user()
{
return $this->belongsTo('App\User', Config::get('constants.LOGGING.ACTION_USER_ID'));
}
/**
* Create Error log
*
* @param $type
* @param array $info
* @return mixed
*/
public static function add($type, $info)
{
$entity = $info['entity'];
$entity_id = $info['entity_id'] ?? '';
$action_user_id = $info['action_user_id'] ?? auth()->user()->id ?? Config::get('constants.SYSTEM_ADMIN');
$action_detail = $info['action_detail'] ?? '';
switch ($type){
case 'create':
$icon = 'fa-plus bg-purple';
$action = 'failed creating';
break;
case 'update':
$icon = 'fa-pencil bg-teal';
$action = 'failed updating';
break;
default:
$icon = $action = 'invalid_flag';
}
Log::error($type.' : '.$entity.' : '.$entity_id.' : '.$action.' : '.$action_detail.' : '.$action_user_id);
return self::create([
Config::get('constants.LOGGING.ENTITY') => $entity,
Config::get('constants.LOGGING.ENTITY_ID') => $entity_id,
Config::get('constants.LOGGING.TYPE') => $type,
Config::get('constants.LOGGING.ICON') => $icon,
Config::get('constants.LOGGING.ACTION') => $action,
Config::get('constants.LOGGING.ACTION_DETAIL') => $action_detail,
Config::get('constants.LOGGING.ACTION_USER_ID') => $action_user_id,
]);
}
}
我相信我可以使用Traits来解决这个问题,但是我看到的示例只是从Traits调用简单函数
如果出现特征,$filleble
、$with
和函数action\u用户
将如何工作
非常感谢您的帮助:)我认为最好的解决方案是创建一个包含重复属性和方法的父类
abstract class LogModel extends Model
{
protected $fillable = [
'entity', 'type', 'icon', 'action', 'action_detail', 'action_user_id'
];
protected $with = ['action_user'];
public function action_user()
{
return $this->belongsTo('App\User', Config::get('constants.LOGGING.ACTION_USER_ID'));
}
// ...
}
现在让您的日志模型继承此类:
class ErrorLog extends LogModel
{
// ...
}
可能的副本,请特别参阅此答案: