【问题标题】:Laravel belongsTo not workingLaravel 属于不工作
【发布时间】:2014-08-19 14:02:10
【问题描述】:

我的应用中有 2 个模型,“用户”和“医学类型”(每个用户属于一个医学类型)。

我使用 belongsTo() 和 hasMany() 在两个模型之间建立了一对多的关系。 hasMany() 关系完美,但belongTo() 不起作用。有谁知道我在哪里做错了吗?

User::find(1)->medicine_type [这不返回任何内容]

MedicineType::find(1)->users [返回用户]

这是模型的代码:

class MedicineType extends Eloquent {

    public function users()
    {
        return $this->hasMany('User');
    }
}


class User extends Eloquent {

    public function medicine_type()
    {
        return $this->belongsTo('MedicineType');
    }
}

这是我的数据库结构:

users:
    id
    name
    medicine_type_id 

medicine_types:
    id
    name

【问题讨论】:

    标签: php laravel orm laravel-4 eloquent


    【解决方案1】:

    型号:

    class User extends Eloquent {
    
        public function medicine_type()
        {
            return $this->belongsTo('MedicineType');
        }
    }
    

    用户控制器.php

    $users= App\User::all();
    
    foreach ($users as $user) {
        echo $user->medicine_type->name;
    }
    

    【讨论】:

    • 虽然这段代码可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因
    【解决方案2】:

    在大多数情况下,排名第一的答案可能是最佳答案。但是,如果您仍然无法加载相关关系并且没有运气..

    还有另一件事可能有效。查看模型的每个表及其索引或外键。就我而言,我更改了表名,但从未更新过相关索引和外键。

    解决方案。

    A:(Feeling Lazy) 只删除关联的索引或外键..

    B:(我不是懒惰的)通过 laravel 迁移删除表并使用适当的外键重新运行 artisan 迁移。

    【讨论】:

      【解决方案3】:

      在我的情况下,相关模型数据已被删除,而 laravel 在一般查询中不会获得软删除数据。要获取软删除数据,您必须使用“withTrashed() 或 onlyTrashed()”。

      您可以在此处查看文档。

      https://laravel.com/docs/5.6/scout#soft-deleting

      【讨论】:

        【解决方案4】:

        我犯了一个愚蠢的错误,没有在关系方法中添加“return”!

        确保恢复关系...显然这不会起作用:

        public function medicineType() 
           {
              $this->belongsTo('MedicineType', 'id');
           }
        

        【讨论】:

        • 也许不显示什么不起作用并显示什么起作用大声笑……这只是令人困惑……
        【解决方案5】:

        我将“medicine_type”更改为“medicineType”,一切正常...

        【讨论】:

          【解决方案6】:

          您的关系不起作用的原因不是因为模型中指定的关系,而是因为 User 模型中的方法命名并且没有指定外键。

          代替:

          public function medicine_type()
          {
              return $this->belongsTo('MedicineType');
          }
          

          用途:

          public function medicineType()
          {
              return $this->belongsTo('MedicineType', 'id');
          }
          

          我希望这对你有用;)

          一切都在一起:

          <?php // app/models/MedicineType.php
          
          class MedicineType extends Eloquent {
          
             // Determines which database table to use
             protected $table = 'medicine_types';
          
             public function users() 
             {
                return $this->hasMany('User');
             }
          
          }
          

          和:

          <?php // app/models/User.php
          
          class User extends Eloquent {
          
             // Determines which database table to use
             protected $table = 'users';
          
             public function medicineType() 
             {
                return $this->belongsTo('MedicineType', 'id');
             }
          
          }
          

          测试是否有效:

          $user = User::find(1);
          return $user->medicineType->name;
          

          这会成功返回相关的medicine_type的名称。

          我希望这可以进一步帮助您;)

          【讨论】:

          • 不错。谢谢梅尔文,这也困扰着我:)
          • 非常感谢。我没有指出该方法必须是驼峰式 - 不允许使用下划线!我在文档中错过了这个吗?
          • 梅尔文,你让我省了很多麻烦!非常感谢。
          • @MelvinKoopmans 为什么一定要指定外键,外键不是medicine_type_id不是id
          • 我同意@briankip - 外键不应该是medicine_type_id 而不是id?它与id 一起工作的事实可能只是在id 匹配时与一些记录的巧合,但生成的查询可能完全错误地加入medicine_types.id=users.id 而不是users.medicine_type_id=medicine_types.id,因为它应该。我总是查看 DebugBar 来验证生成的查询是否正确。
          【解决方案7】:

          也许 Eloquent 查找外键存在问题。试试这个:

          class User extends Eloquent {
          
              public function medicine_type()
              {
                  return $this->belongsTo('MedicineType', 'medicine_type_id');
              }
          }
          

          编辑:

          此外,Eloquent 会尝试查找表“medicinetypes”而不是“medecine_types”,因此您还需要使用 $table 变量来指定它。

          class MedicineType extends Eloquent {
              protected $table = 'medicine_types';
          
              public function users()
              {
                  return $this->hasMany('User');
              }
          }
          

          【讨论】:

          • @HooK 你能提供更多你的类实现吗?
          • “用户”类是 laravel 默认类,它使用默认实现。 (实现 UserInterface、RemindableInterface)
          • @HooK 请看我的编辑。我以为你已经在 MedicineTypes 类中指定了表格。
          • @HooK 好吧,如果您没有收到任何错误,那么您的数据库中有哪些数据可供我们检查?当您找到(1)时,您只是在数据库中找到第一条记录。也许User::find(1)-&gt;medicine_type 在数据库中没有medicine_type。
          • 我再次检查了我的数据库,一切似乎都很好......为了证明这里是“MedicineType::find(1)->users;”的返回[ { id: 1, name: "Yashar", drug_type_id: 1, } ] (用户ID为1)
          猜你喜欢
          • 2018-01-10
          • 2017-11-01
          • 2022-06-29
          • 1970-01-01
          • 2014-03-19
          • 2016-01-20
          • 2018-07-22
          • 2019-01-03
          相关资源
          最近更新 更多