Skip to content

Adds smart relationship access to Eloquent collections and a command to generate model-specific collections in Laravel.

License

Notifications You must be signed in to change notification settings

winavin/magic-collection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Magic Collection for Laravel

Latest Version on Packagist Total Downloads Build Status StyleCI

Magic Collection adds powerful features to Laravel Eloquent collections.
It lets you call model relationships directly on a collection of models, and includes an Artisan command to generate custom collection classes.

✨ Features

  • Call relationships on a collection of models:
    Example: $users->blogs (where $user is a collection)
  • Automatically merges related results using flatMap
  • Safe handling of empty collections
  • Define custom collection classes per model
  • Artisan command to generate collections: php artisan make:collection

Uses

Insted of doing something like below:

In below example, We considerd that User model has blogs ralationship method defined.

$users = User::get();
$blogs = collect();

foreach($users as $user)
{
    $blogs = $blogs->concat($user->blogs);
}

OR

$blogs = User::get()->flatMap(fn ($user) => $user->blogs ?? []);

now you can do this directly on $users

$blogs = User::get()->blogs();

Installation

Via Composer

composer require winavin/magic-collection

Usage

  1. Use the Trait on your models
use Winavin\Collections\UsesMagicCollections;

class User extends Model
{
    use UsesMagicCollections;
}
  1. (Optional) Create a custom collection for a model

php artisan make:collection

This command creates a collection class in the App\Collections folder.

If the collection name matches your model name and path (like YourModelCollection for YourModel Model), it will be used automatically.

You can set any other Collection::class name in your model:

class YourModel extends Model
{
    
    protected function useCollection(): string
    {
        return \App\Collections\CustomYourModelCollection::class;
    }

Absolutely! Here's a "Known Errors" section in proper Markdown format for your README.md, including clear steps to resolve trait method collisions (like the one with newCollection()):


🐞 Known Errors

❌ Trait method collision: newCollection()

You may see this error if you're using another trait or package (like HasRecursiveRelationships) that also defines a newCollection() method:

Trait method Winavin\MagicCollection\Traits\UsesMagicCollections::newCollection has not been applied as App\Models\YourModel::newCollection, because of collision with Other Trait's newCollection method.

✅ How to Fix It

If your model needs to use both MagicCollection and another custom collection (like one from another package), follow these steps:


1. Generate a Custom Collection

php artisan make:collection YourModelCollection

This creates App\Collections\YourModelCollection.


2. Change the Collection's Base Class

In your new collection file, replace:

use Winavin\MagicCollection\Collections\BaseCollection;

class YourModelCollection extends BaseCollection

with the class from the conflicting package, for example:

- use Winavin\MagicCollection\Collections\BaseCollection;
+ use Staudenmeir\LaravelAdjacencyList\Eloquent\Collection as AdjacencyCollection;
+ use Winavin\MagicCollection\Collections\Trait\BaseCollectionTrait;

- class YourModelCollection extends BaseCollection
+ class YourModelCollection extends AdjacencyCollection
{
+    use BaseCollectionTrait;

    // MagicCollection features + AdjacencyCollection features
}

3. Define newCollection() in Your Model and remove Trait

In your model (e.g., App\Models\YourModel), override the method:

- use Winavin\Collections\UsesMagicCollections;

class YourModel extends Model
{
-    use UsesMagicCollections;

+    public function newCollection(array $models = [])
+    {
+       return new \App\Collections\YourModelCollection($models);
+    }
}

This will use your custom collection with both features.


Change log

Please see the changelog for more information on what has changed recently.

License

MIT. Please see the license file for more information.

About

Adds smart relationship access to Eloquent collections and a command to generate model-specific collections in Laravel.

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages