{"id":24341057,"url":"https://github.com/nhalstead/transferable","last_synced_at":"2025-06-21T08:08:01.242Z","repository":{"id":57026474,"uuid":"303216505","full_name":"nhalstead/transferable","owner":"nhalstead","description":"Make model relationships transferable to the same model type","archived":false,"fork":false,"pushed_at":"2022-03-13T17:57:16.000Z","size":22,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-11T20:22:55.531Z","etag":null,"topics":["laravel-eloquent","php-laravel","transferable-relationships"],"latest_commit_sha":null,"homepage":"","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/nhalstead.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}},"created_at":"2020-10-11T21:32:11.000Z","updated_at":"2022-03-12T14:00:07.000Z","dependencies_parsed_at":"2022-08-23T16:11:06.861Z","dependency_job_id":null,"html_url":"https://github.com/nhalstead/transferable","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/nhalstead/transferable","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nhalstead%2Ftransferable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nhalstead%2Ftransferable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nhalstead%2Ftransferable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nhalstead%2Ftransferable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nhalstead","download_url":"https://codeload.github.com/nhalstead/transferable/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nhalstead%2Ftransferable/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261088356,"owners_count":23107683,"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-eloquent","php-laravel","transferable-relationships"],"created_at":"2025-01-18T07:13:37.781Z","updated_at":"2025-06-21T08:07:56.227Z","avatar_url":"https://github.com/nhalstead.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel Transferable Model Relationships\n\nMake laravel model relationships transferable!\n\nThis is perfect to use when you want a model relationships to me assigned to another model (of the same type) and not be deleted or set null.\n\n## Install\n\n```bash\ncomposer require nhalstead/transferable\n```\n\n## How to use\n\nOn the model its self you need to add in the use statement and optionally add in the implements to block the model from being deleted.\n\n```php\n\u003c?php\n\nnamespace App\\Models;\n\nuse nhalstead\\Transferable\\Interfaces\\NoDanglingRelationships;\nuse nhalstead\\Transferable\\Traits\\TransferableRelationship;\nuse Illuminate\\Database\\Eloquent\\Model;\n\nclass User extends Model implements NoDanglingRelationships\n{\n\tuse TransferableRelationship;\n\n\tprotected $transferable = [\n\t\t\"items\"\n\t];\n\n\tpublic function items()\n\t{\n\t\treturn $this-\u003ehasMany(Items::class);\n\t}\n  \n}\n```\n\nBy attaching the interface `NoDanglingRelationships` you enable checks before deletion to ensure no relationships are connected that could be transferred.\n\n### What now?\n\nWe're able to block delete actions if we have relationships that can be transferred still attached, what now?\n\nThis package provides a few extra functions to all models that use `TransferableRelationship` to make things easy and efficient.\nIf a model wants to transfer its relationships to another model you can use the example below:\n\n```php\n\u003c?php\n\n$oldUser = User::find(1);\n$newUser = User::find(2);\n\n// Doing `delete()` will trigger an exception so you need\n//  to transfer any items that are connected to another model.\n\n// Transfer relationships to another item.\n$oldUser-\u003etransferTo($newUser); // Returns the total rows changed.\n\n// Bob's your uncle, now oldUser can be deleted.\n$oldUser-\u003edelete();\n\n?\u003e\n```\n\nWhat makes it efficient? This will use the model's relationship to determine what needs to be updated in the database and runs a query on the\n DB to update the relationships without any extra calls to get all the IDs and detaching things.\n \n### What else?\n\nNothing else, This package offers a few extra method for debugging and collecting information on the transferable relationships, see below:\n\n```php\n\n// Return the number of transferable items attached to this model\n$newUser-\u003ecountTransferable();\n\n// Return boolean if it would have any dangling relationships if deleted.\n// The false param tells it not to throw an Exception\n$newUser-\u003echeckDangling(false);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnhalstead%2Ftransferable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnhalstead%2Ftransferable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnhalstead%2Ftransferable/lists"}