![](/img/trans.png)
[英]How to get a object value nested inside document in a collection in MongoDB PHP
[英]get nested value from object in eloquent collection
我正在将laravel的收银员与stripe&一起从Invoices
集合中使用,并且我正尝试通过其subscription
密钥检索特定的发票:
Collection {#157 ▼
#items: array:1 [▼
0 => Invoice {#162 ▼
#owner: User {#163 ▶}
#invoice: Invoice {#208 ▼
+"id": "in_1BsZF2F3eLup6dYnvs74ij6B"
+"object": "invoice"
...
+"statement_descriptor": null
+"subscription": "sub_CH7UxbgcTCeHLp"
+"subtotal": 1999
+"tax": null
+"tax_percent": null
+"total": 1999
+"webhooks_delivered_at": 1517934260
}
}
]
}
这是user
模型用于检索此Invoices
集合的函数:
public function invoices($includePending = false, $parameters = [])
{
$invoices = [];
$parameters = array_merge(['limit' => 24], $parameters);
$stripeInvoices = $this->asStripeCustomer()->invoices($parameters);
// Here we will loop through the Stripe invoices and create our own custom Invoice
// instances that have more helper methods and are generally more convenient to
// work with than the plain Stripe objects are. Then, we'll return the array.
if (! is_null($stripeInvoices)) {
foreach ($stripeInvoices->data as $invoice) {
if ($invoice->paid || $includePending) {
$invoices[] = new Invoice($this, $invoice);
}
}
}
return new Collection($invoices);
}
每个Invoice
对象都有两个属性, Owner
(当前用户)和实际Invoice
向下钻取每个Invoices
subscription
密钥并对照该值进行核对的正确语法是什么?
我尝试了以下查询,结果为NULL
:
$user->invoices()->where('subscription','sub_CH7UxbgcTCeHLp')->first();
更新
当我转储以下查询$user->invoices()->first()->subscription
它在sub_CH7UxbgcTCeHLp
返回正确的字符串,但是当我尝试使用$user->invoices()->firstWhere('subscription', 'sub_CH7UxbgcTCeHLp')
其结果为NULL
请尝试以下操作:
$id = 'sub_CH7UxbgcTCeHLp'
$user->invoices->first(function ($invoice) use ($id) {
return $invoice->invoice->subscription === $id;
});
尝试使用where时,似乎只是缺少实际发票对象上的嵌套发票属性。 这可能起作用:
$id = 'sub_CH7UxbgcTCeHLp';
$user->invoices()->where(function ($query) use ($id) {
$query->where('invoice.subscription', $id);
})->first();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.