Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Learner's avatar

two or three fields relationship

Is it possible , in one to one relationship more then one field(colums) between two table. What is its solution ? For example , I have student result table and books table. Student result table relate to books table with two fields: one course, second gender. How make relationship between them ? similarly I have other tables

0 likes
5 replies
petrit's avatar

To your student model you should add relation,

class Students extends Model
{
    /**
     * Get the phone record associated with the user.
     */
    public function book()
    {
        return $this->hasOne('App\Books');
    }
}

Than, for calling you should use

$student = Student::find(1);
$student->book;

and it will show the books related to student with id=1.

Learner's avatar

student result gazett model relationship to the table CourseSubjectAllocation which has related books names according to their courses

/**
     * Relationship with coursesubjectallocation one to one with colums CourseID of table coursesubjectallocation
     */
public function coursesubjectallocation()
    {
        return $this->hasOne('App\CourseSubjectAllocation','CourseID', 'Courseid');
    }
here is gender field into the CourseSubjectAllocation table 

and books names into the

/**
     * Relationship with Gazett one to one with colums Courseid of table Gazett_*_*_*
     */
public function gazett()
    {
        return $this->belongsTo('App\Gazett','Courseid', 'CourseID');
    }
here is gender field into the gazett table 

I also want to relationship with another gender field into these models.

How relate models more then one fields ?

@petrit

petrit's avatar

There is no need for more relation, you may access that field (gender) from anywhere, using

$result->gazett->gender
d3xt3r's avatar

@Learner You are not very clear with your question. Elaborate in words, no code required.

Please or to participate in this conversation.