Nick
Nick

Reputation: 713

What is the difference between attributesToArray() and toArray() in Laravel?

Can someone explain the difference between $model->attributesToArray() and $model->toArray() on a model?

I have an issue where a seeder is throwing an error about getCreatedAtAttribute method is not defined and it's complaining due to a toArray() method call. This is what prompted me to try and find out the difference between the two.

After switching to attributesToArray() the seeder runs fine.

Upvotes: 4

Views: 4216

Answers (1)

Jerodev
Jerodev

Reputation: 33196

attributesToArray will only fetch the attributes for the current model.

toArray calls the attributesToArray function, but also adds loaded relations to the array.

Upvotes: 10

Related Questions