It is really hard to read.. What are we looking at?
My guess would be something like
$price = $collection->firstWhere('id', 'TEVELIAI')->paymentTerms->prices->price;
But I am unsure what is objects and what is arrays
Summer Sale! All accounts are 50% off this week.
I have a multidimensional collection and cannot figure out how to get specific values from that collection.
The wanted result would be to extract price not for all of them, but only for specific Item->paymentTerm->prices
As example would be to get price for Item ID 'TEVELIAI' -> 'annual' -> 'guest' -> price, result should be 258.98
^ Illuminate\Support\Collection {#444 ▼ #items: array:2 [▼ 0 => App\Model\Accidents\Dto\AccidentsProductDto {#425 ▼ +id: "TEVELIAI" +paymentTerms: array:1 [▼ 0 => App\Model\Accidents\Dto\AccidentsPaymentTermDto {#424 ▼ +id: "annual" +prices: array:1 [▼ 0 => App\Model\Accidents\Dto\AccidentsPriceDto {#392 ▼ +priceType: "guest" +price: 258.98 } ] } ] } 1 => App\Model\Accidents\Dto\AccidentsProductDto {#422 ▼ +id: "SENELIAI" +paymentTerms: array:1 [▼ 0 => App\Model\Accidents\Dto\AccidentsPaymentTermDto {#407 ▼ +id: "annual" +prices: array:1 [▼ 0 => App\Model\Accidents\Dto\AccidentsPriceDto {#381 ▼ +priceType: "guest" +price: 162.55 } ] } ] } ] #escapeWhenCastingToString: false }
Maybe anyone of you could help with that or point in the right direction on how to get those values, thank you.
@lukutisz Break it up for readability
$product = $collection->firstWhere('id', 'TEVELIAI');
$term = collect($product->paymentTerms)->firstWhere('id', 'annual');
$price = collect($term->prices)->firstWhere('priceType', 'guest')->price;
Please or to participate in this conversation.