Hi am working with an existing Database in Mongo.
In my User Model I have the code below
public function messages()
{
return $this->hasMany('App\Models\Messages', 'foreign_key', 'local_key'); // the keys are setup correctly
}
However this is returning null, I have checked manually to make sure the user has messages.
Any suggestions?
Upvoted!
I'm having the same issue. It looks like something may have changed recently where now it's returning Mongo NumberLong values as doubles (9.0, 12.0, etc) instead of as Integers (9, 12) which means they aren't matching. Haven't found a fix yet, but comparing a working version and a version that's having the same problem you mentioned - it's the only difference I've found between the Laravel collections.
Confirmed the issue is related to laravel-mongodb is creating laravel collections with all mongo NumberLong values as doubles instead of integers.
Temporary workaround will be to access directly using the query builder until the issue is resolved. Ex:
use App\Models\Messages;
// assumes you're user is logged in
$collection = Messages::where('foreign_key', (double) Auth::user()->id)->get();
@SuperSephy nice one thats a good workaround
Also, having this same issue, used a similar workaround.
public function someCollection() {
return Collection::where('foreign_key', new \MongoDB\BSON\ObjectID($this->id))->get();
}
I had this same issue, and after seeing abkothman's workaround it occurred to me that since the relation query was failing to find results due to a type mismatch, I would need to change the data...
I had an ObjectId("...") in my "foreign key" field and the Builder is creating a query that looks for the string representation of the object's ID, and mongodb does not see that as a match.
_[ db.stuff.find({"user_id":ObjectId("123")}) != db.stuff.find({"user_id":"123"}) ]_
Changing the data in the field from ObjectId("...") to a string "..." worked for me.
Now belongsTo() and hasMany() are able to match the appropriate records from the other collections.
_[note: the "_id" fields still contain ObjectId("...") values, just the "something_id" fields need to have the string representation of the same value, instead of true ObjectId()s]_
If you add records using Eloquent, instead manual input, it does store a string not an ObjectId,
eg:
class User extends Model {
function stuff() { return $this->hasMany(Stuff::class); }
}
class Stuff extends Model {
function user() { return $this->belongsTo(User::class);
}
$user = User::find("123");
$stuff = $user->stuff->create($data);
$stuff->user == $user;
@jenssegers In MongoDB 3.2 lookup is working for ObjectId("...") not for string type ids, let me know if you have the solution in you latest release
any update and plan for this ?
Is this fixed?
+1
this seems not fixed, please any solution? i'm still facing the problem
Please fix this ASAP. I want to use hasMany with string object.
Thanks

Most helpful comment
Also, having this same issue, used a similar workaround.
public function someCollection() {return Collection::where('foreign_key', new \MongoDB\BSON\ObjectID($this->id))->get();}