我正试图使用一个基类来抽象我的模型。我有三个继承自同一基础的模型:
- 修理
- 检查
- 采购
我能够成功地创建模型并将其持久化到DB中,但当获取时,我会得到一个空白屏幕,不会抛出任何错误。当我删除$with
属性时,一切似乎都正常。
这是代码:
abstract class ItemType extends Model
{
public $timestamps = false;
protected $with = ['details'];
public function details()
{
return $this->morphOne(Item::class, 'type', 'item_details_type', 'item_details_id', 'id');
}
}
class Repair extends ItemType
{
protected $table = 'repairs';
protected $guarded = ['id', 'created_at', 'updated_at'];
protected $morphClass = self::class;
}
class Inspection extends ItemType {}
class Purchase extends ItemType {}
最后,我决定使用没有抽象类的morphTo
关系来解决这个问题。