我对自定义 shipmentable_type 有疑问。我的结构如下所示:
transfers:
id - integer
name - string
shipment:
id - integer
name - string
shipmentale:
shipment_id - integer
shipmentable_id - integer
shipmentabl_type - enum ( transfer, order, complaint, internet )
现在我的 Transfer 模型 realtion 是这样的:
public function shipments()
{
return $this->morphToMany(Shipment::class, 'shipmentable');
}
问题是,到 shipmentable 表,到 shipmentable_type 列现在是这样的:App/Models/Transfer,但在这种情况下我想强制在那里“转移”。可能吗?
最佳答案
来自docs
By default, Laravel will use the fully qualified class name to store the type of the related model. However, you may wish to decouple your database from your application's internal structure. In that case, you may define a relationship "morph map" to instruct Eloquent to use a custom name for each model instead of the class name:
use Illuminate\Database\Eloquent\Relations\Relation;
Relation::morphMap([
'transfer' => 'App/Models/Transfer'
]);
您可以在AppServiceProvider
的boot
函数中注册morphMap
,或者如果您愿意,可以创建一个单独的服务提供者。
关于php - 具有自定义外键的 Laravel 多态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48435989/