Dov Benyomin Sohacheski Dov Benyomin Sohacheski - 7 months ago 10
PHP Question

Laravel's Eloquent's 'with' array is breaking

I am trying to abstract my models using a single base class. I have three models that inherit from the same base:


  1. Repair

  2. Inspection

  3. Purchase



I am able to successfully create and persist the models to the DB, but when fetching I get a blank screen, no errors are thrown. When I remove the
$with
attribute everything seems to work.

Heres the code:

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 {}

Answer

I ended up using morphTo() relationships without the need for an abstract class.

Comments