Eloquent attitude when model attribute condition

I have two models Studentand one StudentRevisionwith a model Studentrelated hasManyto the model StudentRevision. I determined the ratio hasManyin Studentboth

public function revisions()
{
    return $this->hasMany(
        'StudentRevision',
        'sid'
    );
}

I have a field in the student table (student model) that refers to the current revision of the student from the student_revisions table .

The table structure looks something like this.

students sid srid name ....

student_revisions package srid sid ....

Now I want to determine the relationship hasOneto the model StudentRevisionthat refers to the current revisionone associated with Student. I have currently defined this relationship as:

public function current()
{
    return $this->hasOne(
        'StudentRevision',
        'sid'
    )
    ->where('srid', $this->srid);
}

, $this->srid , .

, , .

+4
1

, . :

public function current(){
    return $this->revisions()->where('srid', $this->srid)->get();
}

$student->current(). relationship :

public function current(){
    return $this->revisions()->where('srid', $this->srid);
}

public function getCurrent(){
    return $this->current()->get();
}

protected $appends = array('current');

. Laravel Docs ( )

:

$student->current; // retrieves the model
$student->current(); // retrieves an instance of the query builder
+1

All Articles