【问题标题】:groupBy with eloquent relationship in laravelgroupBy 在 laravel 中具有雄辩的关系
【发布时间】:2018-05-17 14:52:11
【问题描述】:

我正在尝试为我的发票页面获取数据,其中我针对单个发票编号使用了许多附加行。我获取了数据,但问题是我无法在发票视图页面中显示附加的行。应该写什么查询以获得预期的结果,有人可以帮我吗? 我的数据库表是-

我期待的就像我在箱子里所做的一样——

在我的控制器中,我尝试了类似的操作-

public function orderProformaInvoiceDetails($poi)
{

    $orderProformaInvoiceDetails = OrderProformaInvoice::where('opi_id', $poi)
                                        ->with('buyer', 'buyerJobs', 'buyerOrders')
                                        ->groupBy('invoice_no')
                                        ->orderBy('created_at', 'DESC')
                                        ->get();

    return view('admin.marchendaising.order-proforma-invoices.details', compact('orderProformaInvoiceDetails'));
}

【问题讨论】:

  • 你有什么错误吗?或者您尝试过的方法有什么问题?
  • 我想显示许多 Style Name Description Fabrics Quantity Unit Price Total Price 作为发票,就像我在创建发票期间所做的那样。
  • 你为什么使用groupBy()?您的视图是否应该每个 invoice_no 有一行?
  • 每个invoice_no 有很多行,这就是我需要groupBy() 的原因,我想将'unit_price`、quantitytotal_price 显示为每个invoice_no 的行。

标签: php mysql sql laravel eloquent


【解决方案1】:

您应该对查询结果进行分组:

OrderProformaInvoice::where('opi_id', $poi)
    ->with('buyer', 'buyerJobs', 'buyerOrders')
    ->orderBy('created_at', 'DESC')
    ->get()
    ->groupBy('invoice_no');

@foreach($orderProformaInvoiceDetails as $invoice_no => $details)
    @foreach($details as $detail)
        {{ $detail->quantity }}
    @endforeach
@endforeach

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-12
    • 1970-01-01
    • 2015-03-14
    • 2018-11-28
    • 2015-01-11
    • 2021-06-03
    • 2020-03-26
    相关资源
    最近更新 更多