Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Faster Seeds with Insert and Chunk

Premium
4:04

The Full Lesson is Only for Premium Members

Want to access all of our courses? (29 h 14 min)

You also get:

54 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

HN
Huy Nguyen ✓ Link copied!

hi, instead of chunk collection by 500 records and loop through each chunk, Would it be better if we insert directly 20000 models into database with the code below?

Employee::insert($employees->toArray());

This will produce only one query instead of 40 queries.

Thank you

PK
Povilas Korop ✓ Link copied!

Yes but insert() doesn't take care of timestamps fields and any Eloquent listeners. And 1 long query is not always faster than 40 shorter ones. You should actually test it out, I guess.

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.