Нарушение ограничения целостности Laravel 5.8 - PullRequest
1 голос
/ 11 июля 2019

У меня есть таблица «recurring_payments»

Schema::create('recurring_payments', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('user_id');
            $table->string('card_id');
            $table->string('day');
            $table->integer('amount');
            $table->timestamps();
        });

У меня есть таблица «card»

Schema::create('cards', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('user_id');
            $table->string('token');
            $table->string('last4');
            $table->timestamps();
            $table->softDeletes();
        });

У меня есть отношение «принадлежит» между «recurring_payment» и'card'

class RecurringPayment extends Model
{
    protected $guarded = [];

    public function card()
    {
        return $this->belongsTo(Card::class, 'token');
    }
}

Когда я пытаюсь сохранить повторяющийся платеж

class Card extends Model
{
    use SoftDeletes;

    protected $fillable = [
        'user_id',
        'token',
        'last4',
    ];

    protected $hidden = [
        'user_id',
        'token',
        'created_at',
        'updated_at',
    ];
...

public function storeRecurringPayment($payment, $user)
    {
        $attributes = [
            'user_id' => $user,
            'card_id' => $payment->source['id'],
            'day' => Carbon::now()->day()->addMonths(1)->format('d'),
            'amount' => $payment->amount
        ];
// dd($attributes);
        return $this->recurringPayment()->create($attributes);
    }

    public function recurringPayment()
    {
        return $this->hasOne(RecurringPayment::class);
    }
...
}

, он не будет заполнять 'card_id', он жалуется, что 'card_id' не может быть нулевым, чтоэто то, что я хочу, пока достаточно, но «card_id» заполнен, это массив $ attribute умер и был сброшен незадолго до того, как он был передан 'return $ this-> recurringPayment () -> create ($ attribute);'

array:4 [▼
  "user_id" => 3
  "card_id" => "src_4zcdnrruvgxetpkgxocg6hhk5m"
  "day" => "30"
  "amount" => 10
]

Я указываю в 'recurring_payment', что хочу использовать 'token' в качестве внешнего ключа, а не по умолчанию 'card_id', но я все еще получаю эту ошибку

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'card_id' cannot be null (SQL: insert into 'recurring_payments' ('user_id', 'card_id', 'day', 'amount', 'updated_at', 'created_at') values (3, ?, 30, 10, 2019-07-11 13:53:08, 2019-07-11 13:53:08))

Кто-нибудь может увидеть ошибку, которую я сделал?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...