【问题标题】:How to use softdelete withTrashed in Laravel [duplicate]如何在 Laravel 中使用 softdelete withTrashed [重复]
【发布时间】:2021-04-08 20:55:06
【问题描述】:

我正在laravel 构建一个应用程序,其中我有两个模型公司,并且联系属于以下关系:

class Contact extends Model
{
    use SoftDeletes;

    public function company()
    {
        return $this->belongsTo('App\Company');
    }

}

我也在公司模型中使用软删除,现在假设即使删除了任何公司,我也希望显示联系人以及公司详细信息。

我正在尝试做这样的事情来检索数据:

$allData = Contact::with('company')->withTrashed()->get();

它不起作用,它只显示未删除的公司详细信息。有什么想法可以解决这个问题吗?

【问题讨论】:

标签: laravel laravel-5.4


【解决方案1】:

试试这个解决方案:

  • 与垃圾的联系人:
$allСontacts = Contact::withTrashed()->get();
  • 没有:
$contacts = Contact::all();
  • 获取公司:
$companies = $contacts->company()->withTrashed()->get();
    
$allCompanies = $allСontacts->company()->withTrashed()->get();

【讨论】:

  • 好吧,在这个解决方案中,我必须使用foreach staement,无论我以何种方式获得解决方案。
猜你喜欢
  • 2016-12-24
  • 1970-01-01
  • 2014-11-10
  • 1970-01-01
  • 2019-03-27
  • 1970-01-01
  • 2020-01-25
  • 1970-01-01
  • 2015-11-08
相关资源
最近更新 更多