本文介绍了LARAVEL如何从特征更改Model中的$ fillable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有模特:

use seoTrait;

protected $fillable = [
    'name', 'title', 'description'
];

我在$ fillable中创建了需要"seoMeta"的特征"seoTrait".

I created trait "seoTrait" which need "seoMeta" in $fillable.

现在我添加:

protected $fillable = [
    'name', 'title', 'description', 'seoMeta'
];

但是有可能在特征"seoTrait"中添加一些内容到$ fillable吗?

But is it possible in trait "seoTrait" add something to $fillable ?

推荐答案

This way whenever your model boots up, this method of your Trait will be called ans 'seoMeta' should be added to $fillable

编辑

由于bootSeoTrait是静态方法,因此此答案将不起作用,$this将不可用.

This answer will not work since bootSeoTrait is a static method, $this will not be available.

请参见@DokiCRO回答此处

See @DokiCRO answer here

此答案被接受后,我无法将其删除.

Since this answer is accepted, I can't delete it.

编辑2

Laravel现在支持initialize*方法来初始化模型特征.请参见@OneBigOwnage答案此处

Laravel now supports the initialize* method to initialize model traits. See @OneBigOwnage answer here

这篇关于LARAVEL如何从特征更改Model中的$ fillable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 01:27