laravel 模型关联
假设有两张表
用户表(user)和 联系电话表(phone)
user表有以下字段:
id
name
phone:表有以下字段:
id
user_id
\Model\User //user 模型
public function phone() {
$this->hasOne(Model\Phone, user_id, id); //user_id 是Phone的外键 id是user表的本地key
}
\Model\Phone//phone模型
public function user() {
//默认foreign_key会使用方法名user + _id ,如果父级模型没有使用 id
作为主键,或者是希望用不同的字段来连接子级模型,可以使用other_key
$this->belongsTo(Model\Phone,foreign_key, other_key);
}