Ad

Ignore Softdelete For Polymorphic Relations Doens't Work

I have a log class having a Polymorphic Relationship to the object that where created, updated and deleted.

It's working for creating and updating but not for deleting.

When I call this for a delete log it returns Model not foundbecause it is soft deleted.

public function loggable()
{
    return $this->morphTo();
}

public function getColorAttribute()
{
    return $this->loggable->color;
}

So I need to ignore this softdelete for this call, but how can I do this? The Documentation shows only the case for relationships with one to many.

Another way would be to write an own query but I wanted to ask first if it's possible to do it this way.

Ad

Answer

You can load soft-deleted models with withTrashed():

public function getColorAttribute()
{
  return $this->loggable()->withTrashed()->first()->color;
}
Ad
source: stackoverflow.com
Ad