Pergunta

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!

Foi útil?

Solução

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');
   }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top