Search code examples
phplaravellaravel-5soft-delete

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.


Solution

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

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