https://github.com/zingimmick/laravel-eloquent-relationships
More eloquent relationships for Laravel
https://github.com/zingimmick/laravel-eloquent-relationships
eloquent laravel relationships
Last synced: about 1 month ago
JSON representation
More eloquent relationships for Laravel
- Host: GitHub
- URL: https://github.com/zingimmick/laravel-eloquent-relationships
- Owner: zingimmick
- License: mit
- Created: 2021-11-25T03:32:52.000Z (over 3 years ago)
- Default Branch: 2.x
- Last Pushed: 2025-02-24T15:27:25.000Z (about 2 months ago)
- Last Synced: 2025-02-24T16:33:18.770Z (about 2 months ago)
- Topics: eloquent, laravel, relationships
- Language: PHP
- Homepage:
- Size: 131 KB
- Stars: 8
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Laravel Eloquent Relationships
## Requirements
- [PHP 8.0+](https://php.net/releases/)
- [Composer](https://getcomposer.org)
- [Laravel 8.69+](https://laravel.com/docs/releases)Require Laravel Eloquent Relationships using [Composer](https://getcomposer.org):
```bash
composer require zing/laravel-eloquent-relationships
```## Usage
### BelongsToOne
`BelongsToOne` is based on `BelongsToMany`
#### Difference:
- returns related model instead of collection of models
- returns `null` instead of empty collection of models if the relationship does not exist
- supports return default related model in case the relationship does not exist#### Example:
```php
belongsToOne(User::class)
->wherePivot('status', 1)
->withDefault(function (User $user, self $group): void {
$user->name = 'leader for ' . $group->name;
});
}
}
```### MorphToOne
`MorphToOne` is based on `MorphToMany`
#### Difference:
- returns related model instead of collection of models
- returns `null` instead of empty collection of models if the relationship does not exist
- supports return default related model in case the relationship does not exist#### Example:
```php
morphedByOne(Product::class, 'imageable', 'model_has_images');
}
}
``````php
morphToOne(Image::class, 'imageable', 'model_has_images')->withDefault([
'url' => 'https://example.com/default.png',
]);
}
}
```## License
Laravel Eloquent Relationships is an open-sourced software licensed under the [MIT license](LICENSE).