Closed AmmarRahman closed 6 years ago
This is not a library related issue
@iceheat You may use Eloquent Query Scopes. (They are fully supported by laravel-mongodb
One simple approach is applying a global scope to Foo
model, so every query on it (thus any relation) will return sorted form :)
App\Foo.php
class Foo extends Moloquent
{
protected static function boot()
{
parent::boot();
static::addGlobalScope('sorted_scope', function (Builder $builder) {
$builder->orderBy('created_at', 'desc');
});
}
@pi0
Thank you for the reply. Query Scopes are a good tool, but in the specific case I have more than query adjustment to be done. I did track down the problem to Mongodb\Eloquent\Model@getAttribute
. The problem is that the method searches for relationships before it passes to parent:getAtttribute()
. Only the parent method checks for hasGetMutator($key)
.
My current hack is to add
public function getAttribute($key)
{
// Check for get mutator first
return $this->hasGetMutator($key)
? $this->getAttributeValue($key)
: parent::getAttribute($key);
}
to my BaseModel class
@iceheat Thanks for your nice discovery :) Your workaround is applied on Moloquent. (a36bc4e)
@pi0 I had a look at your commit. Unfortunately, this wouldn't solve the problem since it is run at the end of the method. hasGetMutator
should run before checking for embedded relations.
@iceheat Please checkout 6a536f67277927e5a778ca1fe4ab108d079977ec ;)
That looks perfect!! Don't you think overriding the parent method makes more sense than extending it in this case?
When Having a getMutator and a relation of the same name, standard Eloquent behaviour would prioritise the mutator. However, when the relation is embedOneOrMany, the relation would have the priority over the mutator.
Calling $model->foo on the following would return an ordered collection
However it would have a different behaviour for other relations