简体   繁体   English

在将构造函数添加到模型时,口才无法识别可填充字段

[英]Eloquent does not recognize fillable fields when adding constructor to model

I have model Category as below: 我的模型Category如下:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    /**
     * @var $fillable
     */
    protected $fillable = array('title', 'slug' , 'descriptoin', 'cover', 'logo', 'parent_id');

    /**
     * @var $category_logo_pathp
     */
    protected $category_logo_path ;

    /**
     * @var $category_cover_path
     */
    protected $category_cover_path ;

    /**
     * constructor
     */
    function __construct()
    {
        $this->category_cover_path = public_path() . '/upload/image/category/cover/';
        $this->category_logo_path = public_path() . '/upload/image/category/logo/';
    }

    /**
     * relation with itself
     */
    public function parent()
    {
        return self::where('id', $this->parent_id)->first();
    }

    public function coverPath()
    {
        return $this->category_cover_path;
    }

    public function logoPath()
    {
        return $this->category_logo_path;
    }
}

When inserting a category instance with correct data into database , Eloquent does not recognize fillable fields of the model and then throws Illuminate\\Database\\QueryException but when i remove the constructor from category model , it works fine and fillable fields are recognized using Eloquent. 当将具有正确数据的类别实例插入数据库时​​,Eloquent无法识别模型的可填充字段,然后引发Illuminate\\Database\\QueryException但是当我从类别模型中删除构造函数时,它可以正常工作,并且可使用Eloquent识别可填充字段。 what is causing this? 是什么原因造成的?

For sure, you should run parent constructor and probably it's quite reasonable to use default Model signature, so your constructor should look like this: 当然,您应该运行父构造函数,并且使用默认的Model签名可能是相当合理的,因此您的构造函数应如下所示:

public function __construct(array $attributes = [])
{
   parent::__construct($attributes);
   $this->category_cover_path = public_path() . '/upload/image/category/cover/';
   $this->category_logo_path = public_path() . '/upload/image/category/logo/';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在雄辩的ORM中向模型添加字段 - Adding fields to a model in Eloquent ORM 雄辩的Model :: create()的特定$ fillable和Model-> update()的另一个$ fillable - eloquent specific $fillable for Model::create() and another $fillable for Model->update() Laravel:创造了新的雄辩模型,但是laravel并不认识它 - Laravel: created new eloquent model, but laravel does not recognize it $fillable 不得在 Laravel 中定义(如类 Illuminate\\Database\\Eloquent\\Model) - $fillable must not be defined (as in class Illuminate\Database\Eloquent\Model) in laravel 雄辩的模型添加行,没有“ created_at”和“ updated_at”字段 - Eloquent Model adding row without “created_at” and “updated_at” fields Eloquent - $fillable 的真正用途是什么? - Eloquent - What is $fillable really for? 一个 object 的 Eloquent Model 调用get方法时再次调用构造函数 - An object of Eloquent Model call the constructor again when calling get method laravel模型中的构造函数扩展了雄辩 - Constructor in laravel model extending eloquent 如何在子 class 中扩展 PHP Laravel 模型的可填充字段? - How to extend PHP Laravel model's fillable fields in a child class? Laravel $ fillable在使用 - > update()时是否可以保护? - Does Laravel $fillable protect when using ->update()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM