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

Bono's avatar
Level 1

why i cannot fetch the plans from stripe?

why i cannot fetch the plans from stripe where i code $plans = Plan::all(); ? this is my SubscriptionController 's public method allPlans() public function allPlans() { // Fetch the plans from Stripe $plans = Plan::all();

    // Create the three variables representing the travel packets
    $biking = $plans->where('type', 'biking')->first();
    $hiking = $plans->where('type', 'hiking')->first();
    $walking = $plans->where('type', 'walking')->first();

    // Pass the variables to the view
    return view('stripe.plans', compact('biking', 'hiking', 'walking'));
}
0 likes
1 reply
LaryAI's avatar
Level 58

The issue might be related to the way you are fetching the plans from Stripe. Make sure you have properly set up your Stripe API credentials in your Laravel application.

Here's a possible solution:

  1. First, make sure you have installed the Stripe PHP library by running the following command in your terminal:
composer require stripe/stripe-php
  1. In your SubscriptionController, import the Plan class from the Stripe library at the top of the file:
use Stripe\Plan;
  1. In the allPlans method, you need to initialize the Stripe library by setting your API key. You can do this in the constructor of your controller or in the allPlans method itself. Here's an example of setting the API key in the allPlans method:
public function allPlans()
{
    // Set your Stripe API key
    \Stripe\Stripe::setApiKey('your_stripe_api_key');

    // Fetch the plans from Stripe
    $plans = Plan::all();

    // Create the three variables representing the travel packets
    $biking = $plans->data->where('metadata.type', 'biking')->first();
    $hiking = $plans->data->where('metadata.type', 'hiking')->first();
    $walking = $plans->data->where('metadata.type', 'walking')->first();

    // Pass the variables to the view
    return view('stripe.plans', compact('biking', 'hiking', 'walking'));
}

Make sure to replace 'your_stripe_api_key' with your actual Stripe API key.

Note that the Plan::all() method returns a collection of plans. You can access the plans using the data property of the returned object.

Also, in your code, you are using the type field to filter the plans. If the type field is stored as metadata in your Stripe plans, you need to access it using metadata.type as shown in the code above.

Remember to replace 'stripe.plans' with the actual view name you want to render.

Let me know if this helps!

Please or to participate in this conversation.