首页 > 学院 > 开发设计 > 正文

Laravel Eloquent ORM的hasOne和belongsTo

2019-11-06 07:59:44
字体:
来源:转载
供稿:网友

hasOne()方法有3个参数

public function hasOne($related, $foreignKey = null, $localKey = null)

第一个参数为对应的model,第二个参数默认为model对应的表的外键,第三个参数默认为当前模型对应的表的主键。这个方法的sql语句为

select * from model对应的表 where model对应的表的foreignKey = 当前模型对应的表的localKey

belongsTo()方法有4个参数

public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null)

第一个参数为model,先讲第四个参数,默认为调用belongsTo()的方法名字,第二个参数默认为第四个参数加上_id,第三个参数默认为 model的对应表的主键。相当于sql语句

select * from model对应表 where model对应表的otherKey = 当前模型的表的foreignKey

具体可以通过阅读源码更加详细。


上一篇:maven的认识

下一篇:修改Tomcat端口号

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表