Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
AbdullahKaram-Dev opened this issue Dec 2, 2023 · 1 comment

Comments

@AbdullahKaram-Dev
Copy link

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
Copy link
Author

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),
        ]);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant