Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

skoobi's avatar
Level 13

Cashier Error, name cannot be null

Hi.

I'm just trying to get the subscriptions working but I keep hitting this error,

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null in 

Now I know it looks missing but that's all I'm doing so far is passing the data from vue to the controller and in the controller::

try {
            $user = User::where('id', Auth::id())->with(['pricing'])->firstOrFail();
            $duration = request('duration');
            $stripeToken = request('stripeToken');
           
            // Params 1. Product 2. Plan
            $user->newSubscription($user->pricing->stripe_product_id, $duration . 'months')->create($stripeToken, [
                'email' => $user->email,
            ]);

            return response()->json(['status' => 'Your payment has been successful.'], 200);
        } catch (\Exception $e) {
            event(new NotifyIssues($user, $e));
            $request->session()->flash('status', $e->getMessage());
            $request->session()->flash('alert', 'danger');

            return response()->json(['status' => $e->getMessage()], 422);
        }

The Migration:

Schema::create('subscriptions', function ($table) {
    $table->increments('id');
    $table->unsignedInteger('user_id');
    $table->string('name');
    $table->string('stripe_id')->collation('utf8mb4_bin');
    $table->string('stripe_plan');
    $table->integer('quantity');
    $table->timestamp('trial_ends_at')->nullable();
    $table->timestamp('ends_at')->nullable();
    $table->timestamps();
});

It creates the subscription in stripe and does the payment, but doesn't save to the subscriptions table.

Any ideas? Or more to the point, what am i missing?

0 likes
3 replies
manelgavalda's avatar

When you are saving the subscription to the subscriptions table it is failing because you are not passing the name and it can't be nullable.

    $table->string('name');

I suppose that you need to pass the name here, (or where you are creating the subscription) :

$user->newSubscription($user->pricing->stripe_product_id, $duration . 'months')->create($stripeToken, [
                'name' => $user->name, // Something like this in the subscription creation method
                'email' => $user->email,
            ]);
skoobi's avatar
Level 13

Hi, @globals. Ye, I knew it wasn't passing but wasn't sure why as it was all fine on stripe end.

I figured it out by accident when tidying up the code.

So i done::

/**
* 
* This 
*
*/
$product = $user->pricing->stripe_product_id;
$plan = $duration . 'months';

// Params 1. Product 2. Plan
$user->newSubscription($plan, $plan)->create($stripeToken, [
    'email' => $user->email,
]);

/**
* 
* Instead of this
*
*/
$user->newSubscription($user->pricing->stripe_product_id, $duration . 'months')->create($stripeToken, [
    'email' => $user->email,
]);

And now all works normally.

munazzil's avatar

In your controller use as like below.Because Auth::id() gives null.

        $user = User::where('id', \Auth::user()->id)->with(['pricing'])->firstOrFail();

Please or to participate in this conversation.