我有一个称为Report的模型,它与另一个称为ReportAttachments的模型有很多关系。
在ReportCrudController类中,我有一个以下字段,用于添加或管理附件。
$this->crud->addField([ // Browse multiple
'name' => 'attachments',
'label' => 'Image Attachments',
'type' => 'browse_multiple',
'multiple' => true, // enable/disable the multiple selection functionality
'mime_types' => ['image'], // visible mime prefixes; ex. ['image'] or ['application/pdf']
'model' => 'App\Models\ReportAttachment',
'attribute' => 'id',
'entity' => 'attachments'
]);
但是,由于在编辑过程中出现此错误,我似乎无法使其工作:
“此集合实例上不存在属性[附件]。”
此外,在创建新报告时,会为附件生成空插入查询,并引发异常:
“ SQLSTATE [HY000]:一般错误:1364字段'file'没有默认值(SQL:插入
report_attachments
()个值())”
这是报表模型的代码:
class Report extends Model
{
use CrudTrait;
protected $table = 'reports';
protected $fillable = ['name', 'slug', 'description', 'impression'];
protected $casts = [
'attachments' => 'array',
];
public function attachments()
{
return $this->hasMany('App\Models\ReportAttachment');
}
}
附件模型代码
class ReportAttachment extends Model
{
protected $table = 'report_attachments';
protected $primaryKey = 'id';
protected $fillable = ['file', 'title', 'file_type'];
public $timestamps = false;
public function report()
{
return $this->belongsTo('App\Models\Report');
}
}
我在这里错过什么还是做错什么了吗?预先感谢!
更新: 添加了表定义并更新了模型ReportAttachment的命名空间
CREATE TABLE `reports` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`slug` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`description` longtext COLLATE utf8mb4_unicode_ci,
`impression` enum('0','1','2') COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `reports_name_unique` (`name`),
UNIQUE KEY `reports_slug_unique` (`slug`)
) ENGINE=InnoDB AUTO_INCREMENT=37 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
report_attachments表:
CREATE TABLE `report_attachments` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT '',
`file` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`file_type` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`report_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `report_attachments_report_id_foreign` (`report_id`),
CONSTRAINT `report_attachments_report_id_foreign` FOREIGN KEY (`report_id`) REFERENCES `reports` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;