2017-10-17 7 views
0

私が持っているこの
accountsLaravel三方多対多の雄弁関係

  • ID

contacts
などのデータベース - ID
- ACCOUNT_ID
account_communications
- - ID

と接触モデルACCOUNT_ID:私のコントローラで

class Contact extends Model 
{ 
    public function Account() 
    { 
     return $this->belongsTo('App\Account'); 
    } 
    public function AccountCommunication() 
    { 
     return $this->hasManyThrough('App\AccountCommunication','App\Account'); 
    } 
} 

アカウントモデル

class Account extends Model 
{ 
    public function AccountCommunication() 
     { 
      return $this->hasMany('App\AccountCommunication'); 
     } 
    public function Contact() 
    { 
     return $this->hasMany('App\Contact'); 
    } 
} 

AccountCommunicationモデル

class AccountCommunication extends Model 
{ 
     public function Account() 
    { 
      return $this->belongsToMany('App\Account'); 
     } 
    } 

class ContactController extends Controller 
    { 
    public function index() 
    { 
     $contacts = Contact::with('Account')->with('AccountCommunication')->paginate(10); 
     dd($contacts); 
    } 
    } 

は、このエラー

SQLSTATE [42S22]私を表示します。列が見つかりません: 'フィールドリスト' で1054不明な列 'accounts.contact_id'(SQL:account_communicationsを選択*、accountscontact_idからaccount_communications内部結合accountsaccountsid = account_communicationsaccount_idここで、accountscontact_id(20))

+0

関係を介してそれらをマッピングすることはそう間違っているようです。 'account_communications'中間テーブルですか? –

+0

はい............. – paranoid

答えて

2

に私はあなたがHasManyThrough関係を誤解してhasManyと混合していると思います。あなただけlaravel HasManyThrough例かけ一目見れば、あなたはあなたの構造は、それがために使用されて、その後どのように異なるため、それは実際に

countries 
    id - integer 
    name - string 

users 
    id - integer 
    country_id - integer --> here is the key role for countries posts 
    name - string 

posts 
    id - integer 
    user_id - integer 
    title - string 

のために使用されるものの良いアイデアを得るでしょう。 account_idは両側に存在するので、何を待っていますか?

だけaccount_id e.x

class Contact extends Model 
{ 
    public function Account() 
    { 
     return $this->belongsTo('App\Account'); 
    } 
    public function AccountCommunication() 
    { 
     return $this->hasMany('App\AccountCommunication', 'account_id', 'account_id'); 
    } 
}