我正在使用Laravel 7,目前正在尝试通过使用PHP特性集中一些代码。但是,我想拥有例如还将值添加到protected $attributes
变量或protected $with
变量中。
我为什么要那样?因为我想重复使用代码,而不是告诉每个Laravel模型在哪里使用我的特征来加载关系并将属性添加到模型中。那将是多余的...
我已经想出了一种向protected $attributes
变量添加值的方法。但是,如何将comments
添加到我的protected $with
变量中?
这是将值添加到protected $attributes
变量的代码:
/* Add attributes to model $appends */
protected function getarrayableAppends()
{
$this->appends = array_unique(array_merge($this->appends,['publishedComments']));
return parent::getarrayableAppends();
}
亲切的问候