我嘗試用其他一些領域擴展一個 extintig ˙PHP` Laravel 模型,但我沒有找到正確的解決方案。我使用 PHP 7.1 和 Laravel 6.2這是我的代碼,解釋了我想做什么。原始模型:<?phpnamespace App;use App\Scopes\VersionControlScope;use Illuminate\Database\Eloquent\Model;class Product extends Model{ protected $fillable = [ 'product_id', 'name', 'unit', // ... } // ... relations, custom complex functions are here}正如我想象的如何擴展原始模型:<?phpnamespace App;class ProductBackup extends Product{ protected $fillable = array_merge( parent::$fillable, [ 'date_of_backup', ] ); // ...}但是現在我收到Constant expression contains invalid operations錯誤消息。$fillable我可以在子類中以某種方式擴展原始模型的數組嗎?
1 回答

ITMISS
TA貢獻1871條經驗 獲得超8個贊
在你的子類構造函數中,你可以使用mergeFillable來自 trait 的方法Illuminate\Database\Eloquent\Concerns\GuardsAttributes(自動適用于每個 Eloquent 模型)。
/**
* Create a new Eloquent model instance.
*
* @param array $attributes
* @return void
*/
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->mergeFillable(['date_of_backup']);
}
- 1 回答
- 0 關注
- 124 瀏覽
添加回答
舉報
0/150
提交
取消