簡體   English   中英

忽略softdelete的多態關系不起作用

[英]Ignore softdelete for Polymorphic Relations doens't work

我有一個與創建,更新和刪除對象具有多態關系的日志類。

它適用於創建和更新,但不適用於刪除。

當我將其稱為刪除日志時,它會返回“ Model not found因為它已被軟刪除。

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

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

因此,我需要對此呼叫忽略此軟刪除,但是我該怎么做呢? 該文檔僅顯示一對多關系的情況。

另一種方法是編寫自己的查詢,但我想先問一下是否可以這樣做。

您可以使用withTrashed()加載軟刪除的模型:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM