Laravel 三向多对多 Eloquent 关系
Laravel Three-way Many-to-Many Eloquent Relationship
我有这样的数据库
accounts
- id
- 名字
contacts
- 编号
- account_id
account_communications
- 编号
- 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]: Column not found: 1054 Unknown column 'accounts.contact_id' in 'field list' (SQL: select account_communications
.*, accounts
.contact_id
from account_communications
inner join accounts
on accounts
.id
= account_communications
.account_id
where accounts
.contact_id
in (20))
我觉得你误解了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');
}
}
我有这样的数据库
accounts
- id
- 名字
contacts
- 编号
- account_id
account_communications
- 编号
- 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]: Column not found: 1054 Unknown column 'accounts.contact_id' in 'field list' (SQL: select
account_communications
.*,accounts
.contact_id
fromaccount_communications
inner joinaccounts
onaccounts
.id
=account_communications
.account_id
whereaccounts
.contact_id
in (20))
我觉得你误解了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');
}
}