亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在子類中擴展 PHP Laravel 模型的可填寫字段?

如何在子類中擴展 PHP Laravel 模型的可填寫字段?

PHP
月關寶盒 2023-04-21 14:02:27
我嘗試用其他一些領域擴展一個 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']);

    }


查看完整回答
反對 回復 2023-04-21
  • 1 回答
  • 0 關注
  • 124 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號