{"id":15529346,"url":"https://github.com/ajcastro/insert-update-many","last_synced_at":"2025-04-23T12:42:43.924Z","repository":{"id":56943046,"uuid":"196586417","full_name":"ajcastro/insert-update-many","owner":"ajcastro","description":"Laravel's batch insert or batch update for collection of eloquent models.","archived":false,"fork":false,"pushed_at":"2019-07-12T14:10:12.000Z","size":3,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-29T23:41:19.761Z","etag":null,"topics":["batch-insert","batch-update","eloquent","eloquent-models","laravel","mysql"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ajcastro.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-07-12T13:44:16.000Z","updated_at":"2023-03-12T11:37:20.000Z","dependencies_parsed_at":"2022-08-21T07:50:31.206Z","dependency_job_id":null,"html_url":"https://github.com/ajcastro/insert-update-many","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajcastro%2Finsert-update-many","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajcastro%2Finsert-update-many/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajcastro%2Finsert-update-many/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajcastro%2Finsert-update-many/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ajcastro","download_url":"https://codeload.github.com/ajcastro/insert-update-many/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250436827,"owners_count":21430565,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["batch-insert","batch-update","eloquent","eloquent-models","laravel","mysql"],"created_at":"2024-10-02T11:17:22.121Z","updated_at":"2025-04-23T12:42:43.906Z","avatar_url":"https://github.com/ajcastro.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel Eloquent's Insert/Update Many\n\nLaravel's batch insert or batch update for collection of eloquent models.\nPerform single query for batch insert or update operations.\nThis updates the `created_at` and `updated_at` column of the models and the tables.\nThe method names `insertMany` and `updateMany` is based from the eloquent method name `saveMany`.\nBoth `insertMany` and `updateMany` can accept collection of models or just plain array data.\n\n## Installation\n\n```\ncomposer require ajcastro/insert-update-many\n```\n\n## Usage\n\n### Insert Many\n\nDirectly pass array or collection of models unlike Laravel's built-in `insert()` which only accept arrays.\nThis already sets the `created_at` and `updated_at` columns.\n\n```php\n$users = factory(User::class, 10)-\u003emake();\nUser::insertMany($users);\n```\n\n#### How it works\n\nThe passed collection of models is transformed to its array form, only including fillable attributes, and passed its array\nform to Laravel's native `insert()` method.\n\n### Update Many\n\nUpdate array or collection of models. This perform a single update query for all the passed models.\nOnly the dirty or changed attributes will be included in the update.\nThis updates the `updated_at` column of the models and the tables.\n\n```php\nUser::updateMany($users); // update many models using id as the default key\nUser::updateMany($users, 'id'); // same as above\nUser::updateMany($users, 'username'); // use username as key instead of id\n\n```\n\n#### Specifying which columns to be updated\n\n```php\nUser::updateMany($users, 'id', ['email', 'first_name', 'last_name']);\n```\n\n#### How it works\n\nThis will produce a query like this:\n\n```sql\nUPDATE\n   `users`\nSET\n   `email` =\n   CASE\n      WHEN\n         `id` = '426'\n      THEN\n         'favian.russel@example.com'\n      WHEN\n         `id` = '427'\n      THEN\n         'opurdy@example.org'\n      WHEN\n         `id` = '428'\n      THEN\n         'kaylah.hyatt@example.com'\n      ELSE\n         `email`\n   END\n, `first_name` =\n   CASE\n      WHEN\n         `id` = '426'\n      THEN\n         'Orie'\n      WHEN\n         `id` = '427'\n      THEN\n         'Hubert'\n      WHEN\n         `id` = '428'\n      THEN\n         'Mikayla'\n      ELSE\n         `first_name`\n   END\n, `last_name` =\n   CASE\n      WHEN\n         `id` = '426'\n      THEN\n         'Weissnat'\n      WHEN\n         `id` = '427'\n      THEN\n         'Wiza'\n      WHEN\n         `id` = '428'\n      THEN\n         'Keeling'\n      ELSE\n         `last_name`\n   END\nWHERE\n   `id` IN\n   (\n      426, 427, 428\n   );\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fajcastro%2Finsert-update-many","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fajcastro%2Finsert-update-many","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fajcastro%2Finsert-update-many/lists"}