Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to Set Foreign Key For hasMany Relationship? Please I really need help. #2162

Answered by divine
JenuelDev asked this question in Q&A
Discussion options

So I have this table

// table name: coz_lessons_changes
{
 "_id": ObjectId("5fdd41cfc8c832ba57cdb3a6"),
 "lesson_id": ObjectId("5fd84e467f180d5db02be90f"),
 "to_status": "booking"
}

and I have another table

// table name: coz_lessons
{
 "_id": ObjectId("5fd84e467f180d5db02be90f"),
 "student_uid": NumberInt("3151989"),
 "activated": true,
}

coz_lessons is the main table, and the coz_lessons_changes has a field called lesson_id which is the foreign key from coz_lessons.

how to set the function here so that it will know what to get,
bellow is my initial code but cant make it work.

<?php
namespace App\Models\CozLesson;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class CozLesson extends Eloquent {
 protected $connection = 'utalk_server'; //need to set the connection
 protected $collection = 'coz_lessons'; //pangalan ng table
 public function coz_lesson_changes()
 {
 return $this->hasMany(CozLessonChanges::class,'lesson_id','_id');
 }
 public function getLessonChanges($request) {
 $lesson = CozLesson::find($request->input('id'));
 $data = $lesson->coz_lesson_changes()->get();
 return $data;
 }
}
You must be logged in to vote

Hello,

Try changing "lesson_id": ObjectId("5fd84e467f180d5db02be90f") to string type "lesson_id": "5fd84e467f180d5db02be90f"

Thanks!

Replies: 1 comment 4 replies

Comment options

Hello,

Try changing "lesson_id": ObjectId("5fd84e467f180d5db02be90f") to string type "lesson_id": "5fd84e467f180d5db02be90f"

Thanks!

You must be logged in to vote
4 replies
Comment options

oh yeah, the problem, other team generated it but our team has to get it even if it's as object id. Is there any alternative besides converting to a string? Thanks 😊

Comment options

oh yeah, the problem, other team generated it but our team has to get it even if it's as object id. Is there any alternative besides converting to a string? Thanks 😊

It's actually a bug, however, fixing it will be a breaking change for existing applications so it's basically being delayed.

Keep in mind that this library doesn't correctly use ObjectIds. I don't know what the solution might be...

Thanks!

Comment options

oh thanks..

Comment options

oh thanks..

If you'd like to understand the problems just have a read #1974 (comment)

Thanks!

Answer selected by divine
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /