mercredi 30 septembre 2015

Create using save() is causing database duplicates in Laravel

I have very similar code that is functioning without a hitch elsewhere in my Laravel app, but for some reason the below code is creating two $paypal_object database entries that are identical except for the payment_id field:

DonateController.php

public function mimic()
{
    try {
        //This block is the addOrder function from the pizza tutorial
        $paypal_object = new Paypal();
        //The user who is making the payment
        $paypal_object->user()->associate(Auth::user());
        $paypal_object->amount = 50.00;
        $paypal_object->description = "new subscription";
        $paypal_object->state = $payment->getState();
        $paypal_object->payment_id = $payment->getId();
        $paypal_object->save();
    } catch (Exception $ex) {
        $message = $ex->getMessage();
        $messageType = "error";
    }
    exit;
}

Database Results (with test data) enter image description here

I've condensed the above code from my controller a little. If you'd like to see more of my code, let me know and I'd be happy to provide it. My theory right now is that for some reason my mimic() method is getting run twice, but I'm not sure how to test to see if that's true beyond including this in the above code, but it's not giving me any results this time:

    echo '<script>console.log(' . json_encode("Testing to see how many times this message appears.") . ');</script>';

Even if it is running twice, I'm not sure how that's happening or where to check. I'm guessing it could well be another problem entirely, but I don't know what.

Right now, I'm accessing this method by pinging its route:

Route::get('/paypal/mimic', 'DonateController@mimic');

but for every 1 ping I make, I get 2 database entries as shown in the above image.

Thanks in advance for any help.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire