2

I have a variable $cart which stores the details of product. I want to get a particular field from the cart.

dd($cart) show the following result.

Cart {#437 ▼
  +items: array:1 [▼
    "airports_334_64" => array:4 [▼
      "qty" => 1
      "price" => 1600000
      "duration" => 0
      "item" => array:28 [▼
        "id" => 64
        "created_at" => "2017-05-29 10:24:22"
        "updated_at" => "2017-05-29 10:26:51"
        "title" => "Airport ad"
        "price" => "120000"
        "location" => "Airport T3"
        "city" => "Delhi"
        "state" => "Delhi"
        "rank" => "12"
        "landmark" => "abc"
        "description" => "<p>new</p>"
        "image" => "1496053462.jpg"
        "references" => ""
        "status" => "Available"
        "display_options" => null
        "light_option" => null
        "airportnumber" => null
        "discount" => "1"
        "slug" => null
        "reference_mail" => "[email protected]"
        "airports_id" => "64"
        "area" => "arrival_check_in_hall"
        "displayoption" => "backlit_panel"
        "dimensions" => "7'10"x3'9""
        "optionprice" => "1600000"
        "units" => "8"
        "ad_code" => ""
        "variation_id" => 334
      ]
    ]
  ]
  +totalQty: 1
  +totalPrice: 1600000
}

I want to get the value of reference_mail i.e Cart->items->item->reference_mail

2
  • So you want all reference_mail fields? Because I presume a cart can have multiple items? Commented Jun 1, 2017 at 6:45
  • yes.. all fields Commented Jun 1, 2017 at 7:12

3 Answers 3

4

Use the Collections provided by Laravel:

$items = collect($cart->items);
$reference_mails = $items->map(function($item){
    return $item['reference_mail'];
});

This leaves you with an array of all the reference mails. The behaviour of map is described here

Sign up to request clarification or add additional context in comments.

3 Comments

Getting null value.. Collection {#441 ▼ #items: array:1 [▼ 0 => null ] }
@Omikabira I updated the code. Let me know if this works.
$items = collect($cart->items); $reference_mails = $items->pluck('item'); $abc= $reference_mails[0]['reference_mail'];
1

If you want to access them then you should iterate over the items like so.

foreach ($cart->items as $items) {
    foreach ($items as $item) {
        $email = $item['item']['reference_mail'];
    }
}

If you need to access it directly based on array index then you could do this.

$email = $cart->items['airports_334_64']['item']['reference_mail'];

If you need the list of reference_mail then you do this in your query rather than going through all this. If you have a collection then you can use pluck method to retrieve the list of emails.

2 Comments

Invalid argument supplied for foreach() for third foreach loop
I can't use static value in array
1

This is an old thread, but I'd like to give a typical Laravel answer for this question. You can use Illuminate\Support\Arr to have a clean code, without having to loop and check every dimension of your array:

$referenceMail = Arr::get($cart, 'items.*.reference_mail');

There are a plenty of other very helpful helpers you can pick from, see https://laravel.com/docs/10.x/helpers#arrays-and-objects-method-list.

Helpers names have changed in v5.7, but you have the equivalent (Arr::get() => array_get(), Arr:has() => array_has(),...) if you use an older version of Laravel.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.