Skip to content

Relationships with non-primary keys #1206

Closed
@CleytonBonamigo

Description

@CleytonBonamigo

Hello guys, I`m having some trouble trying to figure out what is wrong with the code.
I have an hasOne relationship between Company and Partner.
Here are my Models:
App\Company.php

use Jenssegers\Mongodb\Eloquent\Model as Model;

class Company extends Model {
    protected $collection = "company";
    protected $primaryKey = "_id";

    public function partner(){
        return $this->hasOne(\App\Partner::class, "com_id");
    }
}

App\Partner.php

use Jenssegers\Mongodb\Eloquent\Model as Model;

class Partner extends Model {
    protected $collection = "partner";
    protected $primaryKey = "_id";

   protected $fillable = ["_id", "com_id", "email"];
}

If set foreign key from hasOne relationship pointing to primary key to model \App\Partner, It works like a charm.
But, If I set foreign key from relationship as com_id, returns NULL.
Both, _id and com_id have the same value.

Am I doing something wrong?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions