{"id":17160834,"url":"https://github.com/korridor/laravel-has-many-sync","last_synced_at":"2025-04-06T08:15:05.867Z","repository":{"id":40257026,"uuid":"202400425","full_name":"korridor/laravel-has-many-sync","owner":"korridor","description":"Allow sync method for Laravel Has Many Relationship.","archived":false,"fork":false,"pushed_at":"2025-03-03T21:00:44.000Z","size":43,"stargazers_count":34,"open_issues_count":1,"forks_count":4,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-03-30T07:10:01.925Z","etag":null,"topics":["laravel","laravel-eloquent","laravel-package","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/korridor.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"license.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-08-14T17:58:00.000Z","updated_at":"2025-03-03T20:58:21.000Z","dependencies_parsed_at":"2024-12-24T12:10:23.493Z","dependency_job_id":"63a8b826-9d70-4a1d-a028-fe1640fc1179","html_url":"https://github.com/korridor/laravel-has-many-sync","commit_stats":{"total_commits":17,"total_committers":4,"mean_commits":4.25,"dds":0.3529411764705882,"last_synced_commit":"43c9e98cf799b13413a1e6a649b06fd7ae688e9a"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/korridor%2Flaravel-has-many-sync","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/korridor%2Flaravel-has-many-sync/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/korridor%2Flaravel-has-many-sync/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/korridor%2Flaravel-has-many-sync/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/korridor","download_url":"https://codeload.github.com/korridor/laravel-has-many-sync/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247451667,"owners_count":20940944,"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":["laravel","laravel-eloquent","laravel-package","php"],"created_at":"2024-10-14T22:26:12.131Z","updated_at":"2025-04-06T08:15:05.849Z","avatar_url":"https://github.com/korridor.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel HasMany Sync\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/korridor/laravel-has-many-sync?style=flat-square)](https://packagist.org/packages/korridor/laravel-has-many-sync)\n[![License](https://img.shields.io/packagist/l/korridor/laravel-has-many-sync?style=flat-square)](license.md)\n[![GitHub Workflow Lint](https://img.shields.io/github/actions/workflow/status/korridor/laravel-has-many-sync/lint.yml?label=lint\u0026style=flat-square)](https://github.com/korridor/laravel-has-many-sync/actions/workflows/lint.yml)\n[![GitHub Workflow Tests](https://img.shields.io/github/actions/workflow/status/korridor/laravel-has-many-sync/unittests.yml?label=tests\u0026style=flat-square)](https://github.com/korridor/laravel-has-many-sync/actions/workflows/unittests.yml)\n[![Codecov](https://img.shields.io/codecov/c/github/korridor/laravel-has-many-sync?style=flat-square)](https://codecov.io/gh/korridor/laravel-has-many-sync)\n\nAllow sync method for Laravel Has Many Relationship.\n\n\u003e [!NOTE]\n\u003e Check out **solidtime - The modern Open Source Time-Tracker** at [solidtime.io](https://www.solidtime.io)\n\n## Installation\n\nYou can install the package via composer with following command:\n\n```bash\ncomposer require korridor/laravel-has-many-sync\n```\n\nIf you want to use this package with older Laravel/PHP version please install the 1.* version.\n\n```bash\ncomposer require korridor/laravel-has-many-merged \"^1\"\n```\n\n**Warning: The 1.\\* versions use a different namespace!**\n\n### Requirements\n\nThis package is tested for the following Laravel and PHP versions:\n\n - 10.* (PHP 8.1, 8.2, 8.3)\n - 11.* (PHP 8.2, 8.3, 8.4)\n - 12.* (PHP 8.2, 8.3, 8.4)\n\n## Usage\n\n### Setup HasMany Relation\n\n```php\nclass Customer extends Model\n{\n    /**\n     * @return HasMany\u003cCustomerContact\u003e\n     */\n    public function contacts(): HasMany\n    {\n        return $this-\u003ehasMany(CustomerContact::class);\n    }\n}\n```\n\nYou can access the sync method like this:\n\n```php\n$customer-\u003econtacts()-\u003esync([\n    [\n        'id' =\u003e 1,\n        'name' =\u003e 'Alfa',\n        'phone_number' =\u003e '123',\n    ],\n    [\n        'id' =\u003e null,\n        'name' =\u003e 'Adhitya',\n        'phone_number' =\u003e '234,\n    ]\n]);\n```\n\nThe sync method accepts an array of data to place on the intermediate table. Any data that are not in the given array will be removed from the intermediate table. So, after this operation is complete, only the data in the given array will exist in the intermediate table.\n\n#### Syncing without deleting\n\nIf you do not want to delete existing data, you may pass  false value to the second parameter in the sync method.\n\n```php\n$customer-\u003econtacts()-\u003esync([\n    [\n        'id' =\u003e 1,\n        'name' =\u003e 'Alfa',\n        'phone_number' =\u003e '123',\n    ],\n    [\n        'id' =\u003e null,\n        'name' =\u003e 'Adhitya',\n        'phone_number' =\u003e '234,\n    ]\n], false);\n```\n\n#### Behaviour for IDs that are not part of the hasMany relation\n\nIf an ID in the related data does not exist or is not in the scope of the `hasMany` relation, the `sync` function will throw a `ModelNotFoundException`.\nIt is possible to modify this behavior with the `$throwOnIdNotInScope` attribute. Per default, this is set to `true`. If set to false, the `sync` function will ignore the Ids instead of throwing an exception.\n\n```php\n$customer-\u003econtacts()-\u003esync([\n    [\n        'id' =\u003e 7, // ID that belongs to a different customer than `$customer`\n        'name' =\u003e 'Peter',\n        'phone_number' =\u003e '321',\n    ],\n    [\n        'id' =\u003e 1000, // ID that does not exist\n        'name' =\u003e 'Alfa',\n        'phone_number' =\u003e '123',\n    ],\n    [\n        'id' =\u003e null,\n        'name' =\u003e 'Adhitya',\n        'phone_number' =\u003e '234,\n    ]\n], throwOnIdNotInScope: false);\n```\n\n#### Example usage in the controller.\n\n```php\nclass CustomersController extends Controller\n{\n    /**\n     * Update the specified resource in storage.\n     *\n     * @param  CustomerRequest  $request\n     * @param  Customer $customer\n     * @return \\Illuminate\\Http\\Response\n     */\n    public function update(CustomerRequest $request, Customer $customer)\n    {\n        DB::transaction(function () use ($customer, $request) {\n            $customer-\u003eupdate($request-\u003eall());\n            $customer-\u003econtacts()-\u003esync($request-\u003eget('contacts', []));\n        });\n\n        return redirect()-\u003eroute('customers.index');\n    }\n}\n```\n\n## Contributing\n\nI am open for suggestions and contributions. Just create an issue or a pull request.\n\n### Local docker environment\n\nThe `docker` folder contains a local docker environment for development.\nThe docker workspace has composer and xdebug installed.\n\n```bash\ndocker-compose run workspace bash\n```\n\n### Testing\n\nThe `composer test` command runs all tests with [phpunit](https://phpunit.de/).\nThe `composer test-coverage` command runs all tests with phpunit and creates a coverage report into the `coverage` folder.\n\n### Codeformatting/Linting\n\nThe `composer fix` command formats the code with [php-cs-fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer).\nThe `composer lint` command checks the code with [phpcs](https://github.com/squizlabs/PHP_CodeSniffer).\n\n## Credits\n\nThis package is a fork of [alfa6661/laravel-hasmany-sync](https://github.com/alfa6661/laravel-hasmany-sync).\n\n## License\n\nThis package is licensed under the MIT License (MIT). Please see [license file](license.md) for more information.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkorridor%2Flaravel-has-many-sync","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkorridor%2Flaravel-has-many-sync","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkorridor%2Flaravel-has-many-sync/lists"}