开发octobercms的插件plugin时需要连接到插件数据库,当直接从routes.php定义的路由插入数据时碰上如下错误:
day
Type Undefined Database/Eloquent/Model.php
而在后台直接添加数据时是不会出现这个错误的,因为octobercms的核心是laravel,参考其过程后发现出现这个错误是由于没设置数据模型的白名单,有同样错误的可以参考修改你的插件目录中的models文件夹中的xxx.php文件(注xxx指的是你的插件名称)。
xxx.php
<?php namespace xxx\xxxxXX\Models;
use Model;
/**
* Model
*/
class XXXXX extends Model
{
use \October\Rain\Database\Traits\Validation;
/*
* Validation
*/
public $rules = [
];
/* 衣咸:就是这个位置——create新增数据时的字段白名单 */
public $fillable = ['day', 'type', 'content', 'title'];
/**
* @var string The database table used by the model.
*/
public $table = 'xxxxx_xxxxx_';
}
完后,就搞惦了。
参考文档
批量赋值(fillable 与guarded)
当通过create方法来保存数据的时候,你需要先在你的模型上定义fillable或guarded属性。
// 批量赋值
白名单
protected $fillable = [‘允许添加的字段名’];
黑名单
protected $guarded = [‘拒绝添加的字段名’];