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

hiren_albiorix's avatar

paymentMethods() return always empty object result

Welcome file Welcome file

  • Cashier Version: ^10.5
  • Laravel Version: ^6.0
  • PHP Version: 7.3.5
  • Database Driver & Version:

Description:

paymentMethods() retrieve always array with empty object.

public function userAllPaymentMethods(Request $request)
    {
        $user = User::find(5);
        $paymentMethod = $user->paymentMethods();
        return response($paymentMethod);
    }

Result : https://www.screencast.com/t/aqenaud77A

Also using Stripe PaymentMethod lib it's works.

public function userAllPaymentMethods(Request $request)
    {
        $user = User::find(5);
        \Stripe\Stripe::setApiKey('{{KEY}}');
        $paymentMethod = \Stripe\PaymentMethod::all([
            'customer' =>  $user->stripe_id,
            'type' => 'card',
        ]);
        
        return response($paymentMethod);
    }

Result : https://www.screencast.com/t/X14ane7WyqS

stackoverflow : Here Cashier Version: ^10.5 Laravel Version: ^6.0 PHP Version: 7.3.5 Database Driver & Version: Description: paymentMethods() retrieve always array with empty object.

public function userAllPaymentMethods(Request $request) { $user = User::find(5); $paymentMethod = $user->paymentMethods(); return response($paymentMethod); } Result : https://www.screencast.com/t/aqenaud77A

Also using Stripe PaymentMethod lib its works.

public function userAllPaymentMethods(Request $request) { $user = User::find(5); \Stripe\Stripe::setApiKey('{{KEY}}'); $paymentMethod = \Stripe\PaymentMethod::all([ 'customer' => $user->stripe_id, 'type' => 'card', ]);

    return response($paymentMethod);
}

Result : https://www.screencast.com/t/X14ane7WyqS

0 likes
4 replies
fylzero's avatar

Try doing dd($paymentMethod);before the return, what does that output?

24 likes
vincenzoraco's avatar

Hi guys, I have the same problem and I found out why it is happening. Well, it is because the paymentMethods() method loop over each payment received from Stripe and it will return it as a class PaymentMethod which then gets converted to an empty object as all the properties are protected.

I don't understand WHY they decided for this behaviour ... It seems to be an 'issue' from a long time.

Unless Jeffrey has a different solution, first of all, for me is not to use this library .. but if you really want to use it, then you need to loop over again like so:

$payment_methods = [];

foreach ($user->paymentMethods() as $paymentMethod) {
    $payment_methods[] = $paymentMethod->asStripePaymentMethod();
}

return response()->json($payment_methods);

Good luck.

1 like

Please or to participate in this conversation.