faustbrian/laravel-collectable

Collectable Polymorphic Eloquent Models for Laravel 5

3.1.0 2018-10-24 02:36 UTC

README

Build Status PHP from Packagist Latest Version License

Installation

Require this package, with Composer, in the root directory of your project.

$ composer require faustbrian/laravel-collectable

To get started, you'll need to publish the vendor assets and migrate:

php artisan vendor:publish --provider="BrianFaust\Collectable\CollectableServiceProvider" && php artisan migrate

Usage

Setup a Model
<?php

namespace App;

use BrianFaust\Collectable\Traits\HasCollections;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use HasCollections;
}
Add an item to the collection.
$user->collection('favorites')->push($model);
Determine if an item exists in the collection for a given model.
$user->collection('favorites')->has($model);
Get the first item from the collection.
$user->collection('favorites')->first();
Retrieve an item from the collection by model.
$user->collection('favorites')->get($model);
Get all of the items from the collection.
$user->collection('favorites')->all();
Remove an item from the collection.
$user->collection('favorites')->forget($model);
Remove all items from the collection.
$user->collection('favorites')->flush();
Set the item-type that should be filtered by.
$user->collection('favorites')->type(Post::class)->all();

Testing

$ phpunit

Security

If you discover a security vulnerability within this package, please send an e-mail to hello@brianfaust.me. All security vulnerabilities will be promptly addressed.

Credits

License

MIT © Brian Faust