php - Laravel Eloquent 'with' 数组正在中断

标签 php laravel eloquent polymorphism

我正在尝试使用单个基类来抽象我的模型。我有三个继承自同一基础的模型:

  1. 修复
  2. 检查
  3. 购买

我能够成功创建模型并将其持久保存到数据库中,但是在获取时我得到一个空白屏幕,没有抛出任何错误。当我删除 $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关系来解决这个问题。

关于php - Laravel Eloquent 'with' 数组正在中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34141635/

相关文章:

javascript - 如何在 $http.post() 之后从 Angular 导航到 php 页面

php - Laravel 目标类 [App\Http\Controllers\App\Http\Controllers\ApiController] 不存在

php - 如何在 Blade foreach循环中获取迭代次数

php - Laravel Eloquent 关系 AVG 有

php - 使用 Eloquent 在 Laravel 中实现 group_by 和 having

php - Eloquent 存储表单数据

php - json_encode 数组的特定部分

php - 将php composer软件包添加到我的git仓库中

php - 使用 file_get_contents 下载内容然后删除

javascript - 仅将事件类添加到单击的类别