Moloquent laravel hasOne & hasMany not working with foreignKey in ObjectId

680 Views Asked by At

I'm Using jenssegers/laravel-mongodb library in my Laravel MongoDB application. However, the hasOne relationship is only working if foreignKey in a string. if it saves as a ObjectId type, the relationship gives an empty result.

MongoDB document user_profile

{
    "_id" : ObjectId("5c40307107928879107d8ee3"),
    "first_name" : "Alert",
    "last_name" : "CB",
    "speciality" : null,
    "company" : ObjectId("5b8cd8bc0176533ff65c4c62"),
    "platform_start_date" : "2019-01-17",
    "updated_at" : ISODate("2019-01-17T13:40:37.000+05:30"),
    "created_at" : ISODate("2019-01-17T13:06:17.000+05:30")
}

Company document

{
    "_id" : ObjectId("5b8cd8bc0176533ff65c4c62"),
    "name" : "ABCDEF",
    "contact" : "Dani",
    "notes" : "Test Note for builders....",
    "updated_at" : ISODate("2018-09-03T12:16:20.000+05:30"),
    "created_at" : ISODate("2018-09-03T12:16:20.000+05:30")
}

In UserProfile model

class UserProfile extends Moloquent
{
public function companies() {
        return $this->hasOne('App\Models\Company', 'company', '_id' );
    }

Currently this companies() give empty array, How to solve this and get the results ? I'm using jenssegers/mongodb: 3.4.2

0

There are 0 best solutions below