所以我有这条线OpportunityController@index()。public function index(){ $opportunities = Opportunity::with([ 'customer', 'province', 'createdBy', 'closedBy', 'status', 'checkedLists', ])->paginate(); return new ApiCollection($opportunities);}所有这些相关模型都在使用SoftDeletestrait。我发现如果那些模型被软删除了,它们就会变成null,你需要调用withTrashed(). 所以这是有效的。public function index(){ $opportunities = Opportunity::with([ 'customer' => function ($query) { return $query->withTrashed(); }, 'status' => function ($query) { return $query->withTrashed(); }, 'province' => function ($query) { return $query->withTrashed(); }, 'createdBy' => function ($query) { return $query->withTrashed(); }, 'closedBy' => function ($query) { return $query->withTrashed(); }, 'checkedLists', ])->paginate(); return new ApiCollection($opportunities);}但是有没有更好(更短/推荐)的方法来做到这一点,而不是像这样重复声明函数?我试过Opportunity::withTrashed([...])->paginate()and Opportunity::with([...])->withTrashed()->paginate,两者都不起作用,仍然返回空值。
1 回答
杨__羊羊
TA贡献1943条经验 获得超7个赞
您可以在建立关系时声明。
public function customer()
{
return $this->belongsTo(Customer::class, 'customer_id')->withTrashed();
}
- 1 回答
- 0 关注
- 541 浏览
添加回答
举报
0/150
提交
取消