1

get a data from an array ..code for getting cart

public function show()
{
    $data = $this->cartService->getCart(auth()->user()->id);
    dd($data);
    
}

Response

 array:2 [
  "cart" => array:9 [
    "id" => 244
    "user_id" => 53
    "total_mrp" => "56000.00"
    "promo" => "HELLO"
    "discount" => "30.00"
    "meta" => {#1575
      +"sub_total": 56000
    }
  ]
  "cart_items" => array:1 [

      ]
    ]
  ]
]

HOW CAN I GET PROMO IN A Variabel??? i tried

return $data->promo;

but error

3
  • as it is array you need to do return $data['cart']['promo']; Commented Feb 24, 2021 at 8:36
  • you have 2 arrays, you need: dd($data['cart']['promo']) Commented Feb 24, 2021 at 8:38
  • ErrorException: Undefined index: promo in file Commented Feb 24, 2021 at 8:38

1 Answer 1

1

You can use collect(). You can read about this function from laravel doc

$data = $this->cartService->getCart(auth()->user()->id);

$cart = collect($data['cart']??[]);
dd($cart->get('promo'));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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