Thinkphp一对多关联查询

[复制链接]
查看244 | 回复0 | 2023-10-7 16:07 | 显示全部楼层 |阅读模式
  1. <?php
  2. namespace app\model;
  3. use think\Model;
  4. class User extends Model
  5. {
  6.     //一对多
  7.     public function profile()
  8.     {
  9.         return $this->hasMany(Profile::class, 'user_id');
  10.     }
  11. }
复制代码
  1. <?php
  2. namespace app\model;
  3. use think\Model;
  4. class Profile extends Model
  5. {
  6. }
复制代码
  1. <?php
  2. namespace app\controller;
  3. use app\model\Role;
  4. use app\model\User as UserModel;
  5. use app\model\Profile as ProfileModel;
  6. class Grade
  7. {
  8.     public function index()
  9.     {
  10.         $user = UserModel::find(19);
  11.         return json($user->profile);
  12.         // echo $user->profile->hobby;
  13.     }
  14. }
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

UID
1
贡献
387
丢币
38902
主题
4607
回帖
116
注册时间
2018-9-25
最后登录
2024-4-27