【问题标题】:laravel 5 Relationship has onelaravel 5 关系有一个
【发布时间】:2017-10-21 03:01:49
【问题描述】:
User table

id | name | user_type | email           | password
1  | John | 1         | john@gmail.com  | something
2  | Roy  | 1         | roy@gmail.com   | something
3  | Rax  | 1         | Rax@gmail.com   | something
4  | Ren  | 1         | ren@gmail.com   | something

AssignUser Table

id | user_id      | assign_to_math | assign_to_chemistry 
1  | 3            | 2               | 1
2  | 4            | 1               | 2

所以,这里是用户表,其中有四个用户可用 现在在分配表的第一行中,Rax 将数学的 tuitoin 分配给 Roy,将化学分配给 John

我想获取 john 和 roy 的详细信息

我在用户模型中尝试过

public function getInfoFormathTutor(){
        return $this->hasOne('App\AssignUser', 'id', 'assign_to_math');
    }

它为我返回空值不知道为什么请帮忙 提前致谢

【问题讨论】:

  • 你的App\AssignUser 模型是什么样的?

标签: laravel


【解决方案1】:

尝试:

public function getInfoFormathTutor(){
    return $this->hasOne('App\AssignUser', 'assign_to_math', 'id');
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-02
    • 1970-01-01
    • 2014-11-30
    相关资源
    最近更新 更多