php中如何链接表,php – Laravel 5中的链接表

我正在编写一个应用程序,需要使用Link表链接在一起的2个表,所以总共有3个表.

//-------

Users

//------

id,

name,

email,

password

//----

UserAccountType

//----

id,

name,

description

//---

UserAccountLink

//---

id,

user_id,

type_id,

用户可以拥有多种帐户类型(管理员,普通帐户,开发人员)…此外,我还设置了外键限制.

唯一的问题是我不明白我将如何链接这些,我尝试了以下内容:

class User extends Model {

// implementation

public function account_type()

{

$this->hasMany('UserAccountTypeLink', 'id', 'id');

}

}

class UserAccountType extends Model {

// Implementation

}

class UserAccountTypeLink extends Model {

// Implementation

public function user_account()

{

return $this->hasOne('UserAccountType', 'type_id', 'id');

}

}

我的预期输出是,例如,用户1具有“Admin”帐户和“Developer”返回.但目前,我似乎无法获得这种理想的输出.我有什么想法我错了?

最佳答案 您不需要中间UserAccountTypeLink表的模型.看看这里获取有关如何在Eloquent:

http://laravel.com/docs/5.1/eloquent-relationships#many-to-many中创建多对多关系的更多信息

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值