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

zoey7907's avatar

Search Results for subscriptions

Hello, I have a project with a database full of users. I am now connecting my project with Stripe to allow users to pay online. I have created the process to create subscriptions for each user. Now I want to be able to, in my project, navigate to the user's profile page (as an admin) and display the subscriptions that this user is already subscribed to. I have used the search feature below:

$stripe = new StripeClient(env('STRIPE_SECRET')); $subscript = $stripe->subscriptions->search(['query' => 'metadata['user_id']:''.$user->id.'' ']);

This successfully returns a whole big array with tons of information. My question is, now how do I use the information that is returned in $subscript ? I just want to capture the id of the subscription so that I can pass it to the view of the next blade.

Thank you

0 likes
1 reply
LaryAI's avatar
Level 58

Hello,

To extract the subscription IDs from the $subscript object and pass them to your Blade view, you can loop through the results and collect the IDs into an array. Then, you can pass this array to the view. Here's how you can do it:

$stripe = new StripeClient(env('STRIPE_SECRET'));

// Search for subscriptions using the Stripe API
$subscript = $stripe->subscriptions->search([
    'query' => 'metadata["user_id"]:"' . $user->id . '"'
]);

// Initialize an array to hold subscription IDs
$subscriptionIds = [];

// Loop through the search results and collect the subscription IDs
foreach ($subscript->autoPagingIterator() as $subscription) {
    $subscriptionIds[] = $subscription->id;
}

// Pass the subscription IDs to the view
return view('user.profile', ['subscriptionIds' => $subscriptionIds]);

In your Blade view (user.profile), you can then display the subscription IDs like this:

<h1>User Subscriptions</h1>
<ul>
    @foreach($subscriptionIds as $id)
        <li>{{ $id }}</li>
    @endforeach
</ul>

This will render an unordered list of subscription IDs on the user's profile page.

Remember to handle any potential exceptions that might occur when interacting with the Stripe API, such as network issues or incorrect credentials. You can do this by wrapping your Stripe API calls in a try-catch block.

Also, ensure that you have the necessary permissions to access the user's subscription information and that you're complying with all relevant privacy laws and Stripe's terms of service.

1 like

Please or to participate in this conversation.