JackieDo / Laravel-Cart

A package used to create and manage carts (such as shopping, recently viewed, compared items...) in Laravel application.
https://jackiedo.github.io/Laravel-Cart
MIT License
152 stars 19 forks source link

how i can get hashItem related to specific model to destroy it ? #46

Open AbdullahKaram-Dev opened 9 months ago

AbdullahKaram-Dev commented 9 months ago

in case i have model id how i can get hashItem related to this model to destory it



public function destroy(Template $template,Cart $cart): JsonResponse
{
      if ($template->hasInCart('soni-designer')) {
          // here i want to remove item from cart i dont have any hash but i have associative model $template
      }
      return $this->responseJson([
          'success' => true,
          'cart' => $cart->name('soni-designer')->getDetails(withActions: false, withTaxes: false),
      ]);
}
AbdullahKaram-Dev commented 9 months ago

i find this way with that pretty package thank you very much but is that the right way i need advice ?



/**
     * @throws InvalidAssociatedException
     * @throws InvalidModelException
     */
    public function destroy(Template $template, Cart $cart): JsonResponse
    {
        if ($template->hasInCart('soni-designer')) {
            $itemHash = $cart->name('soni-designer')->getDetails()->get('items')->filter(function ($item) use ($template) {
                return $item->model->id === $template->getAttribute('id');
            })->first()->get('hash');
            $cart->name('soni-designer')->removeItem($itemHash);
        }
        return $this->responseJson([
            'success' => true,
            'cart' => $cart->name('soni-designer')->getDetails(withActions: false, withTaxes: false),
        ]);
    }