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

davidxd33's avatar

Need help with some logic.

I'm trying to keep a relationship in sync with my products table when a product is being updated. Products can have recurring subscription options with Stripe (monthly, tri-monthly, yearly) and when you edit a product, you can select the recurring options you want to update with the current ones being set already.

For my Stripe plans, I have a table in my database called "plans" and these plans can be mapped to a product in a pivot table (product_plan). Products has a many to many relationship with Plans.

I'm running into logical issues when I need to determine which plans need to be deleted or created when editing a product. I've done this before but not using the new database structure and a many to many relationship (I simply stored the plan id from stripe on the product last time).

I need to:

  1. Get the recurring options from the form.
  2. Get the current plans already mapped to the product.
  3. Compare what's different between the options selected in the form and what's already saved in the database.

Here's what I have so far:

// Check for the plans and compare to existing plans attached to the product.
// If there are any differences, create/delete.

$plans                   = $product->plans;
$requestedPlans = $command->properties['billing'];
$planIds                = [];

foreach($plans as $plan) 
{
    foreach($requestedPlans as $requestedPlan)
    {
        $planIds[] = $this->createOrDeletePlan($plan, $requestedPlan);
    }
}

// with the ids, run $product->plans()->sync($planIds) to create/delete them in the database

protected function createOrDeletePlan($plan, $requestedPlan)
{      
    if(($plan->interval == 'month' && $plan->interval_count == 1) && $requestedPlan !== 'monthly')
    {
         // the plan saved in the db is a monthly one. If no monthly option is provided in the request, 
         // then we need to delete the plan removing it from the product.
    }
    elseif(($plan->interval == 'month' && $plan->interval_count == 3) && $requestedPlan !== 'tri-monthly')
    {
         // the plan saved in the db is a tri-monthly one. If no tri-monthly option is provided in the request, 
         // then we need to delete the plan removing it from the product.
    } 
    elseif($plan->interval == 'year' && $requestedPlan !== 'yearly')
    {
         // the plan saved in the db is a yearly one. If no yearly option is provided in the request, 
         // then we need to delete the plan removing it from the product.
    }

    // do the same but opposite for creating plans...
}

$requestPlan is an array and the keys can either be monthly, tri-monthly, or yearly and the $plan properties mimic those available on the stripe api for plans.

This was extremely hard for me to explain, if you still need more info or have no idea what I'm talking about, let me know.

0 likes
0 replies

Please or to participate in this conversation.