laravel - 如何在laravel中显示软删除的项目?
问题描述
我有一个属于许多产品的订单(数据透视表)和一张属于订单的发票!我使用软删除在我的订单中显示产品(它有效),现在我想在我的发票中显示那些被丢弃的物品!我怎样才能做到这一点?在订单模型中,我有这个:
public function products()
{
return $this->belongsToMany('App\Product','order_product','ord_id','prod_id')->withPivot('prod_quantity','discount')->withTimestamps();
}
在发票控制器中我有这个:
public function show($id)
{
$invoice = Invoice::with('order.products')->find($id);
return response()->json([
'error' => false,
'invoice' => $invoice,
],200);
}
解决方案
利用withTrashed()
public function show($id)
{
$invoice = Invoice::withTrashed()
->with('order.products')
-->find($id);
return response()->json([
'error' => false,
'invoice' => $invoice,
],200);
}
编辑
确保您的模型使用软删除特征。
推荐阅读
- .net-core - MassTransit saga azure 服务总线接收端点设置?
- f# - 如何在 f# 中获得此签名: val sigF : int -> bool -> float -> string
- powershell - 如何使用 Remove-LocalGroupMember 从服务器中删除?
- powershell - Azure DevOps:发布管道中的 PATCH REST 调用返回 (403) 禁止
- wpf - win 7 和 win 10 角半径的不同风格
- php - 结合 imap_open 和 stdin
- c# - 在我的 ASP.NET Core MVC 2.2 项目中显示谷歌日历
- python - 使用 tkinter 和 pyautogui 复制到剪贴板
- clockify - 如何通过 Clockify 中的 API 在工作区添加用户
- python - 如何使用 matplotlib 缩放图形