laravel-mongodb - Issue #3490: [BUG] Cannot instantiate abstract class MongoDB\Laravel\Eloquent\Model

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Gone away
    • Priority: Critical - P2
    • None
    • Affects Version/s: None
    • Component/s: Laravel
    • None
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • None

      theduman has created Issue #3490: [BUG] Cannot instantiate abstract class MongoDB\Laravel\Eloquent\Model in laravel-mongodb. This Jira ticket was filed by GromNaN

      Issue Text:

      • Laravel-mongodb Version: 5.7.0
      • PHP Version: 8.5.4
      • Database Driver & Version: 2.2.1

      Description: Laravel 13.x eloquent models fails that extends MongoDB\Laravel\Eloquent\Model on both fresh install and migrated from 12.x since \Illuminate\Database\Eloquent\HasCollection::resolveCollectionFromAttribute method changes in version 13.x

      Steps to reproduce

      1. Install library on a laravel project that version is 13.x
      2. Create a new model and extend MongoDB\Laravel\Eloquent\Model as documentation states
      3. Try building any query using the model, for example TestModel::all()

      Expected behaviour

      Getting results

      Actual behaviour

      Throws `Cannot instantiate abstract class MongoDB\Laravel\Eloquent\Model `exception

      Since MongoDB\Laravel\Eloquent\Model is abstract, resolveCollectionFromAttribute method in trait \Illuminate\Database\Eloquent\HasCollection throws exception in `new (get_parent_class(static::class))` since we can't instantiate new instance of an abstract class

      {}Laravel 13.x version of HasCollection trait{}

      
      public function resolveCollectionFromAttribute()
      {
      $reflectionClass = new ReflectionClass(static::class);
      $isEloquentGrandchild = is_subclass_of(static::class, Model::class)
      && get_parent_class(static::class) !== Model::class;
      $attributes = $reflectionClass->getAttributes(CollectedBy::class);
      if (! isset($attributes[0]) || ! isset($attributes[0]->getArguments()[0]))
      { return $isEloquentGrandchild ? (new (get_parent_class(static::class)))->resolveCollectionFromAttribute() : null; }
      return $attributes[0]->getArguments()[0];
      }
      
      
      

       

      {}Laravel 12.x version of HasCollection trait{}

      public function resolveCollectionFromAttribute()
      {
      $reflectionClass = new ReflectionClass(static::class);
      $attributes = $reflectionClass->getAttributes(CollectedBy::class);
      if (! isset($attributes[0]) || ! isset($attributes[0]->getArguments()[0]))
      { return; }
      return $attributes[0]->getArguments()[0];
      }
      

            Assignee:
            Jérôme Tamarelle
            Reporter:
            TPM Jira Automations Bot
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: