Skip to content

Support for morphTo relations broken in 4.1 #2657

Closed
@apeisa

Description

@apeisa
  • Laravel-mongodb Version: 4.1.dev
  • PHP Version: 8.2.11
  • Database Driver & Version: 1.16.2 (6.0.3)

Description:

There has been working support for morphTo relations, but that seems to break after upgrading from 4.0 to 4.1.

Steps to reproduce

  1. You need to have model with morphTo relationship. Here is my "Subscription" model (it does extend mongo model, leaving namespaces out for simpllicity):
class Subscription extends Model
{
    use HasFactory;

    public function subscribable(): MorphTo
    {
        return $this->morphTo();
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}
  1. When using the relationship it fails. This is how this is used in our codebase (controller):
$subscription = new Subscription();
$subscription->user()->associate($user);
$subscription->subscribable()->associate($thread);
$subscription->save();

Expected behaviour

New subscription should be saved to database with this kind of values:

{
  "_id": {
    "$oid": "647ee3c2ef95cab3850d4ee9"
  },
  "user_id": "64637d18a21fdb655d09bc83",
  "subscribable_id": "647ee3c2ef95cab3850d4ee8",
  "subscribable_type": "App\\Models\\Thread",
  "updated_at": {
    "$date": "2023-06-06T07:44:02.589Z"
  },
  "created_at": {
    "$date": "2023-06-06T07:44:02.589Z"
  }
}

Actual behaviour

InvalidArgumentException is thrown: First argument of MongoDB\Laravel\Query\Builder::where must be a field path as "string". Got "null"

Here is more detailed stacktrace: https://flareapp.io/share/bP99dGOP

Metadata

Metadata

Assignees

No one assigned

    Labels

    relationsRelations between documents

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions