Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

khalil-mhiri-cyber's avatar

faild cart deletion after transaction

$order->items()->saveMany($orderItems); $cart->items()->delete(); $cart->delete(); these 3 instruction arent working properly

0 likes
5 replies
LaryAI's avatar
Level 58

Certainly! Based on your description, it looks like you want to:

  1. Save multiple order items to an order.
  2. Delete all items from the cart.
  3. Delete the cart itself.

If these instructions aren't working properly, here are a few things to check and a suggested approach:

1. Make Sure Relationships Are Defined Properly

Ensure your relationships in the models are set up correctly. For example:

Order.php

public function items()
{
    return $this->hasMany(OrderItem::class);
}

Cart.php

public function items()
{
    return $this->hasMany(CartItem::class);
}

2. Use Transactions

It's best to wrap these operations in a database transaction to ensure data integrity:

DB::transaction(function () use ($order, $orderItems, $cart) {
    $order->items()->saveMany($orderItems);
    $cart->items()->delete();
    $cart->delete();
});

3. Check for Foreign Key Constraints

If your cart_items table has a foreign key to carts, you need to delete the items before deleting the cart. Your order is correct, but if you have "on delete cascade" set up, you could just delete the cart and its items would be deleted automatically.

Example migration:

$table->foreign('cart_id')->references('id')->on('carts')->onDelete('cascade');

Then, you can simply do:

$cart->delete();

4. Debugging

If deletion is not happening, check for:

  • Errors or exceptions (check your logs)
  • Soft deletes (are you using SoftDeletes trait?)
  • Are you operating on the correct cart instance?

5. Example Full Solution

DB::transaction(function () use ($order, $orderItems, $cart) {
    $order->items()->saveMany($orderItems);

    // If you have cascade deletes, this is enough:
    $cart->delete();

    // Otherwise, do this:
    // $cart->items()->delete();
    // $cart->delete();
});

Summary:

  • Use transactions for safety.
  • Ensure relationships and foreign keys are set up.
  • Use cascade deletes if possible.
  • Check for errors and soft deletes.

If you still have issues, please provide any error messages or more details about what "aren't working properly" means!

khalil-mhiri-cyber's avatar

order.php:
public function items(): HasMany { return $this->hasMany(OrderItem::class); } Cart.php

public function items() { return $this->hasMany(CartItem::class); } handkcheckouttransaction.php:<?php

namespace App\Actions\Webshop;

use App\Mail\OrderConfirmation; use App\Models\Cart; use Laravel\Cashier\Cashier; use App\Models\User; use App\Models\OrderItem; use Stripe\LineItem; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Mail;

class HandleCheckoutSessionCompleted { public function handle($sessionId) { DB::transaction(function () use ($sessionId) { $session = Cashier::Stripe()->checkout->sessions->retrieve($sessionId); $user = User::find($session->metadata->user_id); $cart = Cart::find($session->metadata->cart_id); $order = $user->orders()->create([ 'stripe_checkout_session_id' => $session->id, 'amount_shipping' => $session->total_details->amount_shipping, 'amount_discount' => $session->total_details->amount_discount, 'amount_tax' => $session->total_details->amount_tax, 'amount_subtotal' => $session->amount_subtotal, 'amount_total' => $session->amount_total, 'billing_address' => [ 'name' => $session->customer_details->name, 'email' => $session->customer_details->email, 'city' => $session->customer_details->address->city, 'country' => $session->customer_details->address->country, 'line1' => $session->customer_details->address->line1, 'line2' => $session->customer_details->address->line2, 'postal_code' => $session->customer_details->address->postal_code, 'state' => $session->customer_details->address->state, ], 'shipping_address' => [ 'name' => $session->shipping_details->name, 'email' => $session->shipping_details->email, 'city' => $session->shipping_details->address->city, 'country' => $session->shipping_details->address->country, 'line1' => $session->shipping_details->address->line1, 'line2' => $session->shipping_details->address->line2, 'postal_code' => $session->shipping_details->address->postal_code, 'state' => $session->shipping_details->address->state, ] ]); $lineItems = Cashier::stripe()->checkout->sessions->allLineItems($session->id); $orderItems = collect($lineItems->all())->map(function (LineItem $line) { $product = Cashier::stripe()->products->retrieve($line->price->product);

            return new OrderItem([
                'product_variant_id' => $product->metadata['product_variant_id'],
                'name'           => $product->name,
                'description'           => $product->description,
                'price'           => $line->price->unit_amount,
                'quantity'        => $line->quantity,
                'amount_discount' => $line->amount_discount,
                'amount_subtotal' => $line->amount_subtotal,
                'amount_tax'      => $line->amount_tax,
                'amount_total'    => $line->amount_total,
            ]);
        });
        $order->items()->saveMany($orderItems);
        $cart->items()->delete();
        $cart->delete();
        Mail::to($user)->send(new OrderConfirmation($order));
    });
}

}i try them(Errors or exceptions (check your logs))but it shows nothing .Can i send my source code to you ?

khalil-mhiri-cyber's avatar

when i submit the transaction ,tables of order_items arent updated.They are still empty

khalil-mhiri-cyber's avatar

I have written these codes Order.php

 public function items()
{
    return $this->hasMany(OrderItem::class);
} 

Cart.php

public function items()
{
    return $this->hasMany(CartItem::class);
}

HandleCheckoutSessionCompleted.php

I don't have foreign keys BTW I M USING PHPmyadmin with xampp.

Errors or exceptions (check your logs):Nothing shown.

Soft deletes (are you using SoftDeletes trait?) NO

Are you operating on the correct cart instance? Yes

Can soemone help me or I will share my code with him

Please or to participate in this conversation.