Laravel - 对象不会存储到数据库,当它实际存在时说它是空的

Laravel - Object wont store to database, says its empty when its actually there

我有一个处理 Stripe 支付过程的控制器,当我去存储从 Stripe 取回的订阅信息时,我可以通过 dd() returned 数组结构来验证对象是否存在.但是,当它存储到数据库中时,它会显示以下内容。

// 错误输出;

SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into `subscriptions` (`updated_at`, `created_at`) values (2020-10-06 21:47:42, 2020-10-06 21:47:42))

我从来没有经历过这个...

    // If the subscription status is explicitly active
    if ($subscription->status === 'active') {
        
    // define the needed information to fulfill the process.
    $this->setStripeSubscriptionId($subscription->id);
    $this->setStripeSubscriptionStatus($subscription->status);
    $this->setStripeSubscriptionPlanId($subscription->plan->id);
    $this->setStripeSubscriptionTrialEndOn($subscription->trial_end);
$this->setStripeSubscriptionPlanBillingInterval($subscription->plan->interval);
      ...
    }

保存到数据库的方法

private function storeSubscriptionRecord() {
    $newSub = new StripeSubscription();
    $newSub->user_id = $this->getAccountRecordLocatorID();
    $newSub->name = $this->getPlanName();
    $newSub->stripe_id = $this->getStripeSubscriptionId();
    $newSub->stripe_status = $this->getStripeSubscriptionStatus();
    $newSub->stripe_plan = $this->getStripeSubscriptionPlanId();
    $newSub->quantity = 1;
    $newSub->trial_ends_at = $this->getStripeSubscriptionTrialEndOn();
    $newSub->ends_at = $this->subscriptionTermEnds($this->getStripeSubscriptionPlanBillingInterval());
    $newSub->save();
}

// 我已验证其他对象值中的 user_id 存在并由 dd() 对象验证...

dd($this->dumpValuesToDebug());

调试方法

public function dumpValuesToDebug() {
    return array(
        'UserID' => $this->getAccountRecordLocatorID(),
        'Status' => $this->getStripeSubscriptionStatus(),
        'SubID' => $this->getStripeSubscriptionId(),
        'PlanID' => $this->getStripeSubscriptionPlanId(),
        'TrailEnd' => $this->getStripeSubscriptionTrialEndOn(),
        'BillingInterval' => $this->getStripeSubscriptionPlanBillingInterval()
    );
}

// 将return以下...

array:6 [▼
  "UserID" => "991beba9-2520-410b-a570-2105bd59da86"
  "Status" => "active"
  "SubID" => "sub_I9p70GG3lXbcSU"
  "PlanID" => "price_1HZ9bTD9P7MB4xUmsjQ9suKO"
  "TrailEnd" => null
  "BillingInterval" => "month"
]

// 不知道为什么 sql 在床上拉屎...

我的模型没有任何限制

class Subscription extends Model
{
    // TODO finish model.
    protected $table = 'subscriptions';
    public $user_id;
    public $name;
    public $stripe_id;
    public $stripe_status;
    public $stripe_plan;
    public $quantity;
    public $trial_ends_at;
    public $ends_at;
}

想法?

有没有人有处理这个特定问题的任何想法或经验?

悬崖; 尝试将值存储到数据库中,SQL 表示事务缺少键值。已验证这些值确实存在。

如果您有 public 个属性,那么您将不会设置您的属性($model->user_id = ... 正在设置一个名为 public 属性 的 user_id 而不是属性名为 user_id)。模型的 'attributes'(从数据库中检索的内容,以及用于保存模型的内容)不是 public 属性,它们保存在名为 attributes 的受保护数组中。

Eloquent 模型利用 类 可用的魔术方法来访问和设置无法访问的属性,以获取 __get 和设置 __set 属性.

删除您认为是字段的所有 public 属性。