Skip to content

Conversation

@binaryfire
Copy link
Contributor

This PR ports Laravel's #[CollectedBy] attribute to Hypervel, allowing models to declaratively specify a custom collection class.

Summary

  • Adds #[CollectedBy] attribute for declaring custom collection classes on models
  • Adds HasCollection trait providing the newCollection() method with attribute resolution
  • Modifies Model to use the new HasCollection trait

Usage

use Hypervel\Database\Eloquent\Attributes\CollectedBy;
use Hypervel\Database\Eloquent\Model;

#[CollectedBy(CustomUserCollection::class)]
class User extends Model
{
    // ...
}

// Now User::all() returns a CustomUserCollection
$users = User::all(); // instanceof CustomUserCollection

Changes

New Files

  • src/core/src/Database/Eloquent/Attributes/CollectedBy.php - The attribute class
  • src/core/src/Database/Eloquent/Concerns/HasCollection.php - Trait providing newCollection() with attribute resolution
  • tests/Core/Database/Eloquent/Concerns/HasCollectionTest.php - Tests (9 tests, 16 assertions)

Modified Files

  • src/core/src/Database/Eloquent/Model.php - Added HasCollection trait, removed inline newCollection() method

How It Works

  1. When newCollection() is called on a model, the HasCollection trait checks for a #[CollectedBy] attribute
  2. If found, the specified collection class is used
  3. If not found, falls back to the default Hypervel\Database\Eloquent\Collection
  4. Collection classes are cached per model class for performance

Notes

  • PHP attributes are not inherited - child classes need their own #[CollectedBy] attribute if they want a custom collection
  • This follows Laravel's implementation pattern

Ports Laravel's #[CollectedBy] attribute to Hypervel, allowing models to
declaratively specify a custom collection class.

- Add CollectedBy attribute class
- Add HasCollection trait with newCollection() and attribute resolution
- Modify Model to use HasCollection trait
- Add tests (9 tests, 16 assertions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant