【发布时间】:2018-03-28 22:21:36
【问题描述】:
我有这样的数据库accounts
- 身份证
- 姓名
contacts
- 身份证
- account_idaccount_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');
}
}
账户通信模型
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内部连接@987654332 中选择account_communications.*、accounts.contact_id@ onaccounts.id=account_communications.account_id其中accounts.contact_idin (20))
【问题讨论】:
-
这种关系似乎很不对劲。
account_communications是中间表吗? -
是的......
标签: php laravel laravel-5 eloquent laravel-eloquent