Search code examples
phplaravelstripe-paymentslaravel-authentication

Create customer on laravel registration


All I want to do is to create a stripe customer account for every registered user on Laravel. The problem I'm facing right now is that $new variable isn't passing to User::create function.

I'm trying to edit RegisterController.php, here is create() function I'm using:

public function create(array $data)
{
    \Stripe\Stripe::setApiKey('sk_test_XXXXXXXXXXXXXXX');

    $new = \Stripe\Customer::create([
        'email' => $data['email'],
        'name' => $data['name'],
        'description' => "Contribee.com platform's user",
    ]);

    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'stripe_code' => $new->id,
        'password' => Hash::make($data['password']),
    ]);
}

I've tested this out in my other controller, and $new outputs generated ID. Everything worked fine.


Solution

  • https://laravel.com/docs/6.x/eloquent#mass-assignment

    You may also use the create method to save a new model in a single line. The inserted model instance will be returned to you from the method. However, before doing so, you will need to specify either a fillable or guarded attribute on the model, as all Eloquent models protect against mass-assignment by default.

    Anything not permitted as fillable gets discarded.