我的模型类别
如下:
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;
}
}
当将具有正确数据的类别实例插入数据库时,Eloquent 无法识别模型的可填充字段,然后抛出 Illuminate\Database\QueryException
但当我从类别模型中删除构造函数时,它可以工作使用 Eloquent 可以识别精细字段和可填写字段。这是什么原因造成的?
最佳答案
当然,您应该运行父构造函数,并且使用默认模型签名可能是相当合理的,因此您的构造函数应如下所示:
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/';
}
关于php - 将构造函数添加到模型时,Eloquent 无法识别可填充字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44359161/