使用save()创建会导致Laravel中的数据库重复

时间:2015-10-01 00:28:01

标签: php laravel laravel-4 laravel-5 laravel-5.1

我的代码非常相似,在我的Laravel应用程序的其他地方正常运行,但由于某种原因,下面的代码创建了两个$ paypal_object数据库条目,除了payment_id字段外,它们是相同的:

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;
}

数据库结果(包含测试数据) enter image description here

我从控制器中浓缩了上面的代码。如果您想查看更多我的代码,请告诉我,我很乐意提供。我现在的理论是,由于某种原因,我的mimic()方法已经运行了两次,但是我不知道如何测试以确定在上面的代码中是否包含了这个,但是这次没有给我任何结果:

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

即使它运行了两次,我也不确定这是怎么回事或在哪里检查。我猜这完全是另一个问题,但我不知道是什么。

现在,我通过ping路由来访问此方法:

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

但是对于我所做的每1次ping,我得到2个数据库条目,如上图所示。

Paypal模型:

class Paypal extends Eloquent
{
    /**
     * Get the user that made the paypal payment.
     */
    public function user()
    {
        # Defines an inverse one-to-many relationship
        return $this->belongsTo('User');
    }
}

用户模型:

public function paypal(){
        # User has many paypal payments - although just one subscription
        # Defines a one-to-many relationship
        return $this->hasMany('Paypal');
    }

提前感谢您的帮助。

0 个答案:

没有答案