mongodb / laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)
https://www.mongodb.com/docs/drivers/php/laravel-mongodb/
MIT License
7.01k stars 1.43k forks source link

Relation hasMany will not work #1902

Open inkvc17 opened 4 years ago

inkvc17 commented 4 years ago

Hi, I am a MongoDB rookie and need your help. I have 2 simple models:

Model merchant

Bildschirmfoto 2020-01-05 um 21 03 27

Controller

Bildschirmfoto 2020-01-05 um 21 04 47

collection merchants

Bildschirmfoto 2020-01-05 um 20 58 36

collection comments

Bildschirmfoto 2020-01-05 um 20 58 44

If I only want to query the merchant, I don't have any problems, but if I want to use hasMany (relation to comments) it will not work

**Illuminate\Database\Eloquent\Collection {#371 ▼

items: []

}**

Hope you can help me - many thanks

quachtinh761 commented 4 years ago

Hi bro, i also faced the same problem, then i saw this issue: https://github.com/jenssegers/laravel-mongodb/issues/1383 Hope this can help you

Smolevich commented 4 years ago

@inkvc17, what is the version laravel-mongodb, php driver and mongo?

divine commented 4 years ago

@Smolevich this is related to this PR https://github.com/jenssegers/laravel-mongodb/pull/1523

inkvc17 commented 4 years ago

Hi @Smolevich , I use

mongodb/mongodb 1.5.1 jenssegers/mongodb v3.6.1 Laravel Framework 6.6.2

MongoDB Driver 1.6.1

Hope you can help

xiaoxiaoyu93 commented 4 years ago

try this:

replace hasMany method in your Model:

            $instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey

to

            $instance->newQuery(), $this, $foreignKey, $localKey

such as

    /**
     * Define a one-to-many relationship.
     *
     * @param  string  $related
     * @param  string  $foreignKey
     * @param  string  $localKey
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function hasManyMongo($related, $foreignKey = null, $localKey = null)
    {
        $instance = $this->newRelatedInstance($related);

        $foreignKey = $foreignKey ?: $this->getForeignKey();

        $localKey = $localKey ?: $this->getKeyName();

        return $this->newHasMany(
            $instance->newQuery(), $this, $foreignKey, $localKey
        );
    }

in my project's case :

    public function items(){
//        return $this->hasMany(KjzxCheckTaskItem::class,'task_id','id');

        $instance = $this->newRelatedInstance(KjzxCheckTaskItem::class);

        return $this->newHasMany(
            $instance->newQuery(), $this, 'task_id', 'id'
        );
    }
tailtq commented 3 years ago

I figure it out myself. In this repository, HasMany class extends EloquentHasMany of Laravel. And in the EloquentHasMany class, Laravel uses getIdAttribute Mutator [1] to get a string ObjectId (This method was defined in src/Jenssegers/Mongodb/Eloquent/Model.php line 51) instead of a real ObjectId. Then obviously the query will fail due to the type comparison.

I have no idea how to resolve this in a creative way. Hopefully, this may help someone who is trying to figure out what's going on.

[1] getParentKey. src/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php line 83

juanmanavarro commented 3 years ago

At the one side model:

public function getIdAttribute($value = null)
{
    return $value;
}

A bit tricky, but works. I don't know if this brokes something but my tests are passing.

waiel commented 3 years ago

@juanmanavarro worked like a charm with me, Thanks

rizalreza commented 2 years ago

@juanmanavarro thanks, its worked for me.

cyberwizard-dev commented 3 months ago

At the one side model:

public function getIdAttribute($value = null)
{
    return $value;
}

A bit tricky, but works. I don't know if this brokes something but my tests are passing.

I know this is an old thread but, this works for hasMany. On my Laravel nova table, on the other hand, the id is missing and this leads to a 404 when I click on any record on the table.