[英]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.