{"id":30598151,"url":"https://github.com/effectra/laravel-model-operations","last_synced_at":"2026-01-20T16:27:11.421Z","repository":{"id":311055304,"uuid":"1042301652","full_name":"effectra/laravel-model-operations","owner":"effectra","description":"A Laravel package for model operations.","archived":false,"fork":false,"pushed_at":"2025-10-23T20:19:05.000Z","size":127,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-10-23T22:18:19.024Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"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/effectra.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-08-21T19:50:35.000Z","updated_at":"2025-10-23T20:18:04.000Z","dependencies_parsed_at":"2025-08-21T22:13:19.005Z","dependency_job_id":"abaaa277-6c44-4afc-94e1-1cda245251c3","html_url":"https://github.com/effectra/laravel-model-operations","commit_stats":null,"previous_names":["effectra/laravel-model-operations"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/effectra/laravel-model-operations","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/effectra%2Flaravel-model-operations","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/effectra%2Flaravel-model-operations/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/effectra%2Flaravel-model-operations/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/effectra%2Flaravel-model-operations/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/effectra","download_url":"https://codeload.github.com/effectra/laravel-model-operations/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/effectra%2Flaravel-model-operations/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28607059,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-20T16:10:39.856Z","status":"ssl_error","status_checked_at":"2026-01-20T16:10:39.493Z","response_time":117,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":[],"created_at":"2025-08-29T22:11:19.925Z","updated_at":"2026-01-20T16:27:11.404Z","avatar_url":"https://github.com/effectra.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# LaravelModelOperations\n\n`LaravelModelOperations` is a lightweight package that provides reusable **traits** for performing common Eloquent model operations in Laravel, such as creating, updating, deleting, and handling bulk actions.\n\nIt helps keep your controllers and services clean by centralizing repetitive model logic into reusable traits.\n\n---\n\n## Installation\n\nYou can install the package via Composer:\n\n```bash\ncomposer require effectra/laravel-model-operations\n```\n\n---\n\n## Features\n\n* 🎯 Simplified model operations (`create`, `createMany`, etc.)\n* 🔄 Bulk operations with error handling\n* 🛡️ Strong typing \u0026 exceptions for better safety\n* 🧩 Easy to extend with your own logic\n\n---\n\n## The `UseCreate` Trait\n\nThe `UseCreate` trait provides methods to **create single or multiple model records** in a clean and reusable way.\n\n### Importing the trait\n\n```php\nuse LaravelModelOperations\\Traits\\UseCreate;\n\nclass UserService\n{\n    use UseCreate;\n\n    protected string $model = \\App\\Models\\User::class;\n}\n```\n\n---\n\n### Creating a single record\n\n```php\n// In a controller or service:\n$userService = new UserService();\n\n// Example request validation\n$request = new \\Illuminate\\Http\\Request([\n    'name' =\u003e 'John Doe',\n    'email' =\u003e 'john@example.com',\n    'password' =\u003e bcrypt('secret'),\n]);\n\n// Create user\n$created = $userService-\u003ecreate($request);\n\nif ($created) {\n    $user = $userService-\u003egetModelCreated();\n    dump(\"User created:\", $user-\u003etoArray());\n}\n```\n\n✅ **What happens here?**\n\n* The data is validated (`$request-\u003evalidated()`) before being used.\n* A new `User` is created and stored in `$userService-\u003egetModelCreated()`.\n* If creation fails, it simply returns `false`.\n\n---\n\n### Creating with default attributes\n\nSometimes you want to **add default values** during creation:\n\n```php\n$created = $userService-\u003ecreate(\n    $request,\n    ['status' =\u003e 'active'] // default values\n);\n```\n\nThis will merge defaults with the validated request before saving.\n\n---\n\n### Adding a callback after creation\n\nYou can pass a closure that runs after successful save:\n\n```php\n$created = $userService-\u003ecreate($request, [], function ($user) {\n    // Send a welcome email\n    \\Mail::to($user-\u003eemail)-\u003esend(new \\App\\Mail\\WelcomeMail($user));\n});\n```\n\n---\n\n### Creating multiple records at once\n\n```php\n$request = new \\Illuminate\\Http\\Request([\n    [\n        'name' =\u003e 'User 1',\n        'email' =\u003e 'user1@example.com',\n        'password' =\u003e bcrypt('secret'),\n    ],\n    [\n        'name' =\u003e 'User 2',\n        'email' =\u003e 'user2@example.com',\n        'password' =\u003e bcrypt('secret'),\n    ]\n]);\n\n$success = $userService-\u003ecreateMany($request);\n\nif ($success) {\n    echo \"All users created successfully!\";\n} else {\n    echo \"Some users failed to create. Failed index: \" . $userService-\u003emodelFailedIndex;\n}\n```\n\n✅ **What happens here?**\n\n* Each item in the request array is passed to `create()`.\n* If all succeed → returns `true`.\n* If one fails → returns `false` and stores the **failed index** for debugging.\n\n---\n\n## Exception Handling\n\nWhen bulk creation fails, a `ManyOperationException` can be thrown with the index of the failed item.\n\n```php\ntry {\n    $success = $userService-\u003ecreateMany($request);\n} catch (\\ManyOperationException $e) {\n    logger(\"Failed at index: \" . $e-\u003egetIndex());\n}\n```\n\n---\n\n## License\n\nThis package is open-sourced under the [MIT license](LICENSE).\n\n---","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feffectra%2Flaravel-model-operations","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feffectra%2Flaravel-model-operations","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feffectra%2Flaravel-model-operations/lists"}