Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/webparking/laravel-type-safe-collection
An extension of the Laravel collection that forces a single type.
https://github.com/webparking/laravel-type-safe-collection
Last synced: about 1 month ago
JSON representation
An extension of the Laravel collection that forces a single type.
- Host: GitHub
- URL: https://github.com/webparking/laravel-type-safe-collection
- Owner: webparking
- License: mit
- Created: 2019-04-26T14:25:43.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-09-10T10:22:19.000Z (over 4 years ago)
- Last Synced: 2024-11-25T08:42:45.212Z (about 1 month ago)
- Language: PHP
- Homepage:
- Size: 49.8 KB
- Stars: 37
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
Laravel Type-Safe CollectionPHP is getting more mature and allows us to program strong typed in new ways with each new version, however we still lack the option to have generic lists/arrays. This package aims to provide such a thing in the meantime.
The `TypeSafeCollection` provided by this package will make sure that any object within it is the object you expect.
## Installation
Add this package to composer.```
composer require webparking/laravel-type-safe-collection
```## Usage
```php
/**
* @method \ArrayIterator|User[] getIterator()
* @method User|null first()
*/
class UserCollection extends TypeSafeCollection
{
protected $type = User::class;
}
``````php
class User extends Model
{
public function newCollection(array $models = []): UserCollection
{
return new UserCollection($models);
}
}
```All queries on User that would result in a Collection will now result in a UserCollection.
```php
get_class(User::all()) // UserCollection
get_class(User::where('type', '=', 'admin')->get()) // UserCollection
get_class(User::where('id', '=', 1)->first()) // User
```## Licence and Postcardware
This software is open source and licensed under the [MIT license](LICENSE.md).
If you use this software in your daily development we would appreciate to receive a postcard of your hometown.
Please send it to: Webparking BV, Cypresbaan 31a, 2908 LT Capelle aan den IJssel, The Netherlands