Thursday, 15 August 2013

php - Laravel Eloquent - Inserting multiple objects at once -



php - Laravel Eloquent - Inserting multiple objects at once -

i'm creating multiple (sometimes hundreds, thousands) eloquent objects reading csv file.

take loop instance:

$cities = []; while ($city = readnextcity()) { $cities[] = $city; } // ok how save $cities without calling $city->save() on each?

we know inserting multiple rows in 1 query much more efficient inserting 1 row per query. however, there way insert multiple eloquent objects @ 1 time translated 1 query? (if not, don't mind writing it, don't want duplicate efforts).

i think can utilize query builder this.

$cities = array(); $i=0; while ($city = readnextcity()) { $cities[$i++] = array('city' => $city); } db::table('city')->insert($cities);

hope help.

php mysql laravel eloquent

No comments:

Post a Comment