簡體   English   中英

laravel 集合嵌套“where”

[英]laravel collection nested "where"

假設以下集合:

 [
   order
    -id
    -receiverAddress
    -...
    relations
        transactions
            -id
            -transaction_id_external
            - ...
 ]

我嘗試在集合中使用以下過濾器:

    $isNotEmpty = $orders->filter(function ($order) use ($receivedPaymentDetails) {
        return $order->transactions
            ->where('transaction_id_external', $receivedPaymentDetails->txid)
            ->where('order.receiverAddress', $receivedPaymentDetails->address)
            ->isNotEmpty();
    })->isNotEmpty();

這似乎不起作用,知道如何過濾父集合項 order.receiverAddress 嗎?

由於您想根據交易關系進行過濾,因此您必須對交易表執行某種形式的連接查詢

最簡單的方法是將查詢放在 whereHas 回調中

$isNotEmpty = $orders->filter(function ($order) use ($receivedPaymentDetails) {
            return $order->whereHas('transactions', function ($query) use ($receivedPaymentDetails) {
                $query->where('transactions.transaction_id_external', $receivedPaymentDetails->txid);
                $query->where('order.receiverAddress', $receivedPaymentDetails->address);
            })
                ->isNotEmpty();
        })->isNotEmpty();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM