【发布时间】:2023-02-17 07:02:38
【问题描述】:
在关于 Relations -> Morphs 的 Laravel 9 文档中,它说:
“关键约定 如有必要,您可以指定多态子模型使用的“id”和“type”列的名称。如果这样做,请确保始终将关系的名称作为第一个参数传递给 morphTo 方法。通常,此值应与方法名称相匹配,因此您可以使用 PHP 的功能持续的:
/**
* Get the model that the image belongs to.
*/
public function imageable()
{
return $this->morphTo(__FUNCTION__, 'imageable_type', 'imageable_id');
}
“
在我的应用程序中,表格列是“kind”和“item_id”
根据此文档,这似乎应该适用于我的上传模型:
public function uploadable()
{
return $this->morphTo(__FUNCTION__, 'kind', 'item_id');
}
我的产品型号有:
public function documents()
{
return $this->morphMany(Upload::class, 'uploadable');
}
然而,当我调用我的 $model->documents() 函数时,我得到了这个 SQL 错误:
“SQLSTATE[42S22]:未找到列:1054 ‘where 子句’中的未知列‘uploads.uploadable_id’(SQL:从uploads中选择*,其中uploads.uploadable_id在(5467)和uploads.@ 987654328@ = App\Models\Product 和 uploads.deleted_at 为空)”
因此,该功能似乎甚至无法正常工作。
【问题讨论】:
标签: polymorphism relationship laravel-9