https://github.com/nhalstead/transferable
Make model relationships transferable to the same model type
https://github.com/nhalstead/transferable
laravel-eloquent php-laravel transferable-relationships
Last synced: 12 months ago
JSON representation
Make model relationships transferable to the same model type
- Host: GitHub
- URL: https://github.com/nhalstead/transferable
- Owner: nhalstead
- License: mit
- Created: 2020-10-11T21:32:11.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-03-13T17:57:16.000Z (over 4 years ago)
- Last Synced: 2025-03-11T20:22:55.531Z (over 1 year ago)
- Topics: laravel-eloquent, php-laravel, transferable-relationships
- Language: PHP
- Homepage:
- Size: 21.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Laravel Transferable Model Relationships
Make laravel model relationships transferable!
This 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.
## Install
```bash
composer require nhalstead/transferable
```
## How to use
On 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.
```php
hasMany(Items::class);
}
}
```
By attaching the interface `NoDanglingRelationships` you enable checks before deletion to ensure no relationships are connected that could be transferred.
### What now?
We're able to block delete actions if we have relationships that can be transferred still attached, what now?
This package provides a few extra functions to all models that use `TransferableRelationship` to make things easy and efficient.
If a model wants to transfer its relationships to another model you can use the example below:
```php
transferTo($newUser); // Returns the total rows changed.
// Bob's your uncle, now oldUser can be deleted.
$oldUser->delete();
?>
```
What 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
DB to update the relationships without any extra calls to get all the IDs and detaching things.
### What else?
Nothing else, This package offers a few extra method for debugging and collecting information on the transferable relationships, see below:
```php
// Return the number of transferable items attached to this model
$newUser->countTransferable();
// Return boolean if it would have any dangling relationships if deleted.
// The false param tells it not to throw an Exception
$newUser->checkDangling(false);
```