laravel 学习(25)一对一关联模型
发布日期:2021-06-28 19:45:51
浏览次数:2
分类:技术文章
本文共 1079 字,大约阅读时间需要 3 分钟。
实例 user表 和 user_detail
模型User
public function detail(){ return $this->hasOne(UserDetail::class,'user_id'); }
UserDetail模型
class UserDetail extends Model{ protected $table = 'user_detail'; protected $primaryKey = 'id'; protected $guarded = ['id'];}
控制器调用
public function index() { $user = User::find(1); return $user->detail; }}
结果
{ "id":1, "user_id":1, "hobby":"喜欢小姐姐", "created_at":"2020-06-20T14:58:56.000000Z", "updated_at":"2020-06-20T14:58:58.000000Z", "deleted_at":null}
副表调主表
class UserDetail extends Model{ protected $table = 'user_detail'; protected $primaryKey = 'id'; protected $guarded = ['id']; public function user(){ return $this->belongsTo(User::class,'id'); }}
//一对一 副表调主表$detail = UserDetail::find(1);$detail->user;return $detail->user;
结果
{ "id":1, "name":"谢谢", "phone":"15029149794", "full_name":"谢谢", "status":1, "access_token":"wfwefwefwffaf", "created_at":"2020-06-20T11:11:30.000000Z", "updated_at":"2020-06-20T11:11:35.000000Z", "deleted_at":null}
转载地址:https://blog.csdn.net/xxpxxpoo8/article/details/106871721 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月26日 15时47分06秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Java开发必用的工具包
2019-04-29
世界500强公司要求员工必须熟练掌握的七种工作方法
2019-04-29
九个做事的顺序,你会更加优秀
2019-04-29
史上最详细的Hadoop环境搭建
2019-04-29
最近经历的一些大数据(Spark/Hadoop)面试题
2019-04-29
Hadoop MapReduce原理及实例
2019-04-29
Java 集合系列目录(Category)
2019-04-29
redis永久设置或取消密码
2019-04-29
Git .gitignore配置学习
2019-04-29
git remote 删除添加的远程地址
2019-04-29
LeetCode 338. 比特位计数
2019-04-29
LeetCode 190. 颠倒二进制位
2019-04-29
LeetCode 268. 丢失的数字
2019-04-29
LeetCode 231. 2 的幂
2019-04-29
LeetCode 191. 位1的个数
2019-04-29
LeetCode 476. 数字的补数
2019-04-29
LeetCode 342. 4的幂
2019-04-29
El表达式
2019-04-29
springboot banner打印,控制台springboot图案怎么来的
2019-04-29
linux shell內建命令区分--type
2019-04-29