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

chadhurin's avatar

Laravel Stripe-php exception when billable model has a relationship when creating a new subscription

Had an issue creating a new subscription for a billable model.

My company model is the billable model and followed the docs setting CASHIER_MODEL=App\Models\Company in the .env file.

When getting to the code:

$res = $company->newSubscription('subscription', 'sub_premium_monthly')->create($paymentMethod->id); I get an error:

exception: "Error"
file: "/Users/chad/Sites/FW/vendor/stripe/stripe-php/lib/Util/Util.php"
line: 232
message: "Object of class Illuminate\Database\Eloquent\Relations\HasManyThrough could not be converted to string"

I have middleware checking the ownership of the company (if belongs to an employee making the request) so by the time it gets to the controllers method to add a new subscription is has the employees relationship attached and I get the exception.

Took me a while to figure out why but finally I just unset the employees after the check in the middleware... but when checking if the company was already subscribed $company->subscribed('subscription'); company had a new relations of subscriptions and needed to unset that relationship.

My question is am I doing something wrong because it seems odd to need to have a "clean" model with zero relationships before newSubscriptionmethod can be called?

I'm on PHP 7.4 - Laravel 8.23.1 - cashier 12.6.3

Edit: Here are the last few lines of my log

[2021-01-20 18:38:27] local.ERROR: Object of class Illuminate\Database\Eloquent\Relations\HasManyThrough could not be converted to string {"userId":17,"exception":"[object] (Error(code: 0): Object of class Illuminate\Database\Eloquent\Relations\HasManyThrough could not be converted to string at /Users/chad/Sites/FW/vendor/stripe/stripe-php/lib/Util/Util.php:232)
[stacktrace]
#0 /Users/chad/Sites/FW/vendor/stripe/stripe-php/lib/Util/Util.php(172): Stripe\Util\Util::urlEncode(Object(Illuminate\Database\Eloquent\Relations\HasManyThrough))
#1 /Users/chad/Sites/FW/vendor/stripe/stripe-php/lib/HttpClient/CurlClient.php(225): Stripe\Util\Util::encodeParameters(Array)
#2 /Users/chad/Sites/FW/vendor/stripe/stripe-php/lib/ApiRequestor.php(422): Stripe\HttpClient\CurlClient->request('post', 'https://api.str...', Array, Array, false)
#3 /Users/chad/Sites/FW/vendor/stripe/stripe-php/lib/ApiRequestor.php(119): Stripe\ApiRequestor->_requestRaw('post', '/v1/subscriptio...', Array, Array)
#4 /Users/chad/Sites/FW/vendor/stripe/stripe-php/lib/ApiOperations/Request.php(63): Stripe\ApiRequestor->request('post', '/v1/subscriptio...', Array, Array)
#5 /Users/chad/Sites/FW/vendor/stripe/stripe-php/lib/ApiOperations/Create.php(25): Stripe\ApiResource::_staticRequest('post', '/v1/subscriptio...', Array, Array)
#6 /Users/chad/Sites/FW/vendor/laravel/cashier/src/SubscriptionBuilder.php(274): Stripe\Subscription::create(Array, Array)
#7 /Users/chad/Sites/FW/app/Http/Controllers/SubscriptionController.php(112): Laravel\Cashier\SubscriptionBuilder->create('pm_1IBlWPCZJTjT...')
#8 /Users/chad/Sites/FW/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): App\Http\Controllers\SubscriptionController->store(Object(App\Models\Company), Object(Illuminate\Http\Request))
#9 /Users/chad/Sites/FW/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): Illuminate\Routing\Controller->callAction('store', Array)
#10 /Users/chad/Sites/FW/vendor/laravel/framework/src/Illuminate/Routing/Route.php(254): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(App\Http\Controllers\SubscriptionController), 'store')
0 likes
2 replies
chadhurin's avatar

I figured out the cause of the issue but don't understand why. I'll reply with my findings in case others have this issue a little later.

Please or to participate in this conversation.