2

In Laravel, When I run the following query, it returns a row with null values.

//Cards.php

public function __construct(array $attributes = []) {
    $this->gateway = StripeGateway;
} 


protected $fillable = ['user_id', 'card_id', 'customer_id', 'exp_year', 'exp_month', 'funding', 'brand', 'last4'];

public function createNewCardFromCustomer($user_id, $customer)
    {

        $result = $this->create([
            'user_id' => $user_id,
            'customer_id' => $customer->id,
            'card_id' => $customer['sources']['data'][0]->id,
            'exp_year' => $customer['sources']['data'][0]->exp_year,
            'exp_month' => $customer['sources']['data'][0]->exp_month,
            'funding' => $customer['sources']['data'][0]->funding,
            'brand' => $customer['sources']['data'][0]->brand,
            'last4' => $customer['sources']['data'][0]->last4
        ]);

        return $result;

    }

Even the Model static create method receives the right parameters. And I've taken care of the mass assignment also.

6
  • So createNewCardFromCustomer is a method inside the cards model? And how exactly are you calling this method? Just making sure I understood your situation before trying to give you a response. Commented Feb 2, 2016 at 21:35
  • yes, it's a function within the Cards Model. I'm calling it like $card = (new Cards())->createNewCardFromCustomer($user->id, $customer); Commented Feb 2, 2016 at 21:36
  • do dd($customer) check you are definitely passing something in and your references are right Commented Feb 2, 2016 at 23:25
  • I've checked and I'm getting all values. Also, in my constructor for this model, I've $this->gateway = StripeGateway; will this affect anything? Commented Feb 2, 2016 at 23:27
  • Can you show your constructor? Commented Feb 3, 2016 at 12:44

1 Answer 1

2

I posted this on Laracasts too :)

Anyway, you have to change your constructor to this:

public function __construct(array $attributes = []) {
    $this->gateway = StripeGateway;
    parent::__construct($attributes);
}

You are overriding the Model's base constructor, which changes its default behavior. Laravel uses the constructor for a lot of things (create method, relationships, etc.).

The base model's constructor function does several things, but one very important part of it is that it accepts an array to fill out its attributes as can be seen here:

public function __construct(array $attributes = [])
{
    $this->bootIfNotBooted();

    $this->syncOriginal();

    $this->fill($attributes);
}

So, after you set your gateway property, you should call the parent's constructor function and pass the attributes.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah :) Just wanted someone to post the answer here as well :) Thanks mate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.