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

Viewed 60

I have a controller that handles a Stripe payment process, when I go to store the subscription information retrieved back from Stripe, I can validate the objects exist by dd() the returned array structure. However when it goes to store into the database it says the following.

// Errors out;

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))

Ive never experienced this...

    // 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);
      ...
    }

Save to database method

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

// I have validated that the user_id among the other object values is there and validated by dd() the object...

dd($this->dumpValuesToDebug());

Debug method

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

// Will return the following...

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

// Not sure why sql shits the bed...

I dont have any limitations on my model

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

Ideas?

Does anyone have any ideas, or experience dealing with this particular issue?

Cliffs; Attempt to store values into the database, SQL says the transaction is missing key values. Validated that the values do actually exist.

1 Answers

If you have public properties then you will not be setting your attributes ($model->user_id = ... is setting a public property named user_id not the attribute named user_id). The 'attributes' of the model (what is retrieved from the database, and what is used to save the model) are not public properties, they are held in a protected array named attributes.

Eloquent models make use of the magic methods available to classes for accessing and setting inaccessible properties for getting, __get, and setting, __set, the attributes.

Remove all the public properties you think are fields.

Related