我们可以自定义morphMany
$type
变形许多
public function morphMany($related, $name, $type = null, $id = null, $localKey = null)
通过使用官方文档示例。我可以通过将第三个参数 $type
与 commentable_object
一起传递,将数据库架构中的 commentable_type
更改为 commentable_object
comments
id - integer
body - text
commentable_id - integer
commentable_type - string
但是,morphToMany
函数没有this参数。
这里是morphToMany的源代码
public function __construct(Builder $query, Model $parent, $name, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName = null, $inverse = false)
{
$this->inverse = $inverse;
$this->morphType = $name.'_type';
$this->morphClass = $inverse ? $query->getModel()->getMorphClass() : $parent->getMorphClass();
parent::__construct(
$query, $parent, $table, $foreignPivotKey,
$relatedPivotKey, $parentKey, $relatedKey, $relationName
);
}
它总是使用
$this->morphType = $name.'_type';
创建morphType
。为什么 morphToMany
不允许更改 $type
列的名称?
最佳答案
目前还不可能。 A similar question was asked在 Laravel github issues ,结论是不能定制。
如果您希望在未来的 Laravel 版本中看到这一点的改变,我建议您在 Laravel Ideas 上提交问题。 repo 协议(protocol)。
或者,您可以 fork Laravel 存储库并自定义源代码以满足您的需求。
关于laravel - 是否可以自定义 laravel eloquent morphToMany 的 name_type 列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49716077/