Reputation: 35
i want to join "_id" in user table with "user_id" in Comment table. please help me to resolve this issue. can you give me proper solution for hasone relationship between two model
User table in mongodb
{
"_id" : ObjectId("59784d8f82956de05700003a"),
"updated_at" : ISODate("2017-07-27T05:02:04.000Z"),
"created_at" : ISODate("2017-07-26T08:06:39.000Z"),
}
Comment Table in mongo db
{
"_id" : ObjectId("59787c5782956ddc57000041"),
"updated_at" : ISODate("2017-07-28T13:05:17.000Z"),
"created_at" : ISODate("2017-07-26T11:26:15.000Z"),
"replycomments" : [
{
"user_id" : "592cfcced5429ba9fc091fa0",
"created_at" : ISODate("2017-07-28T13:05:17.000Z")
}
]
}
public function user()
{
return $this->hasOne('App\Models\Users', '_id', 'user_id');
}
Upvotes: 0
Views: 953
Reputation: 256
in your User Model add a function
public get_comment(){
return $this->hasOne('App\Comment','user_id','_id');
}
suppose I am fetching data of a user from user table and I want the comment from the comment table now to get the data :
$user = User::findOrFail($id);
now print the value to check :
dd($user->get_comment->replycomments);
Upvotes: 1