Question

table MemberOwner:

id | name | time

table Member:

id | sex | age

talbe MemberOwner_Member:

id | ownerid | memberid

and this is my relationship-defined code:

class MemberOwner extends Eloquent {

    public function members()
    {
        return $this->belongsToMany('Member','MemberOwner_Member','?','?');
    }
}

look at the question mark above,how to fill with it?thank you,I've tried

ownerid,memberid

and

memberid,ownerid

but neither of them works,I need you help ,thanks again!

Was it helpful?

Solution

Try this:

class MemberOwner extends Eloquent {

public function members()
{
    return $this->belongsToMany('Member','MemberOwner_Member','foreign key','localkey');
  }
}

Here we are specifying this in MemberOwner model and for MemberOwner_Member table foreign key would be ownerid and if you want to specify local id then it would be id which is primary key there. Use this:

 class MemberOwner extends Eloquent {

   public function members()
   {
    return $this->belongsToMany('Member','MemberOwner_Member','ownerid');
  }
}

And this will work also if you want to specify local key.

 class MemberOwner extends Eloquent {

    public function members()
   {
      return $this->belongsToMany('Member','MemberOwner_Member','ownerid','id');
   }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top