所以我有这条线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,两者都不起作用,仍然返回空值。
杨__羊羊