所以我有這條線OpportunityController@index()。public function index(){ $opportunities = Opportunity::with([ 'customer', 'province', 'createdBy', 'closedBy', 'status', 'checkedLists', ])->paginate(); return new ApiCollection($opportunities);}所有這些相關(guān)模型都在使用SoftDeletestrait。我發(fā)現(xiàn)如果那些模型被軟刪除了,它們就會變成null,你需要調(diào)用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);}但是有沒有更好(更短/推薦)的方法來做到這一點,而不是像這樣重復(fù)聲明函數(shù)?我試過Opportunity::withTrashed([...])->paginate()and Opportunity::with([...])->withTrashed()->paginate,兩者都不起作用,仍然返回空值。
1 回答

楊__羊羊
TA貢獻1943條經(jīng)驗 獲得超7個贊
您可以在建立關(guān)系時聲明。
public function customer()
{
return $this->belongsTo(Customer::class, 'customer_id')->withTrashed();
}
- 1 回答
- 0 關(guān)注
- 611 瀏覽
添加回答
舉報
0/150
提交
取消