我的模型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;
}
}
当将具有正确数据的类别实例插入数据库时,Eloquent无法识别模型的可填写字段,然后抛出Illuminate\Database\QueryException
但是当我从类别模型中删除构造函数时,它工作正常并且使用可填充字段进行识别雄辩。造成这种情况的原因是什么?
答案 0 :(得分:2)
当然,你应该运行父构造函数,并且使用默认模型签名可能是合理的,所以你的构造函数应该是这样的:
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/';
}