How can we perform bulk database insertions in Laravel using Eloquent ORM?
I want to accomplish this in Laravel: https://stackoverflow.com/a/10615821/600516 but I am getting the following error.
SQLSTATE[HY093]: Invalid parameter number: mixed named and positional parameters.
Or when using Laravel's Factories (creating 10 users):
I searched many times for it, finally used custom
timestamps
like below:Maybe a more Laravel way to solve this problem is to use a collection and loop it inserting with the model taking advantage of the timestamps.
EDIT:
Sorry for my misunderstanding. For bulk inserting this could help and maybe with this you can make good seeders and optimize them a bit.
You can just use
Eloquent::insert()
.For example:
UserSubscription
is my model name. This will return "true" if insert successfully else "false".This is how you do it in more Eloquent way,