mongodb / laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)
https://www.mongodb.com/compatibility/mongodb-laravel-integration
MIT License
6.99k stars 1.42k forks source link

Model getters does not works with ->project() #2021

Open Siebov opened 4 years ago

Siebov commented 4 years ago

Description:

Getter mutator attribute does not works with ->project() statement in query.

Steps to reproduce

1.Define getNameAttribute($value) { return 'Test name'; } getter function in User (or any other). 2.Create a query using

$someModel
->with(['user' => function ($q) { 
    $q->project([
        '_id' => 1, 
        'name' => 1
]) 
}])

Expected behaviour

The value of user.name in the results should the value of the name after getter, because it's model based Moloquent query and not a raw one.

Actual behaviour

The value of user.name is a raw value from DB, without any getter mutation.

Siebov commented 4 years ago

In the same time, it works if I use ->with('user') without any additional statements.

Siebov commented 4 years ago

If I use "hide" projection

$someModel
->with(['user' => function ($q) { 
    $q->project([
        'email' => 0
]) 
}])

and there is getter for name - it works quite as expected.