简体   繁体   English

laravel 集合嵌套“where”

[英]laravel collection nested "where"

Assume the following collection:假设以下集合:

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

I tried to use the following filter on the collection:我尝试在集合中使用以下过滤器:

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

It seems like this doesn't work, any idea how I can filter on the parent collection item order.receiverAddress?这似乎不起作用,知道如何过滤父集合项 order.receiverAddress 吗?

Since you want to filter based on the transactions relations, you must perform some form of join query with your transactions table由于您想根据交易关系进行过滤,因此您必须对交易表执行某种形式的连接查询

Easiest way for you would be to place your queries in a whereHas callback最简单的方法是将查询放在 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