I have two tables in my Laravel project: teacher_subject
and subject
. The teacher_subject
table has three columns: id
, teacher_token
and subject_token
. And the subject table has three columns: id
, subject_token
and subject_name
. teacher_subject
table’s subject_token
referencing the subject
table’s subject_token
. Now, I want to get the subject_name
of subject_token
wherever the teacher_token
is 1. I want to get it by Laravel’s relation method. But, I don’t know how to write the function in teacher_subject
model and controller to get the subject_name
. Please tell me how to do a many-to-many relation or hasManyThrough
relation without a third table?
2
Answers
I’m not sure if I understood your question correctly, but is this what you’re trying to achieve?
Well, you certainly don’t need
hasManyThrough()
as far as I know,hasMany()
would suffice. The following is a solution with just two tables. Try it out, I’m not entirely sure if that’s what you want.Now, you can do the following