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

redlik's avatar

Cannot save price variable to table

I have a transaction model with column to save the amount being charged during the transaction. The transaction means buying "credits" for a certain amount, so from my radio tag I read number of credits and using switch case I select the right amount, ie if 5 credits is selected $amount = 39.99 etc.

$amount works fine when making a new charge object with stripe but for some reason I cannot save this variable to the table.

I tried setting it up as decimal, float with 2 decimal places or just float without any parameters and nothing works.

I've dd'ed the variable inside the function and it shows as begin float so what's wrong with my setup? Here's my code snipped:

switch ($credit) {

        case 5:

            $amount = 39.99;

            break;

        case 15:

            $amount = 99.99;

            break;

        case 50:

            $amount = 249.99;

            break;

        default:

            $amount = 39.99;

    }

and my table setup: $table->float('amount', 8, 2)->nullable(); I had to add nullable to make the function work at all, obviously I don't want that column be blank.

0 likes
7 replies
tykus's avatar

Can you show the remainder of the code in the method (especially after you assign to $amount)?

redlik's avatar

I just call transaction->save() after successful stripe charge. All table columns are called exactly like my payment form fields plus the amount variable taken from switch. All fields are saved no problem, just this one doesn't.

tykus's avatar

But $amount gets assigned to $transaction->amount how?

redlik's avatar

Right now it doesn't directly - I presumed since I have all fields named the same it would pick up like the form data

tykus's avatar
tykus
Best Answer
Level 104

No! You have a variable $amount, which exists separately to the Request object, and/or the Transaction instance. If you show us your remaining code, we can show you where to make that assignment

redlik's avatar

It works! I've added the assignment just before the save command. Thnx a lot!

Please or to participate in this conversation.