Laravel属于不工作 [英] Laravel belongsTo not working

查看:91
本文介绍了Laravel属于不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有2个模型,用户和 'MedicineType'(每个用户属于一个MedicineType)。



我使用belongsTo()和hasMany()两个模型之间的一对多关系。 hasMany()关系正常工作,但belongsTo()不起作用。有没有人知道我在哪里犯了一个错误?


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



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


以下是模型的代码:

  class MedicineType extends Eloquent {

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


类用户扩展Eloquent {

public function medicine_type()
{
return $这 - >属于关联( 'MedicineType');
}
}

这里是我的数据库结构:

 用户:
id
名称
medicine_type_id

medicine_types:
id
name


解决方案

不工作不是因为模型中指定的关系,而是由于用户模型中的方法命名而没有指定外键。



而不是:

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

使用:

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

我希望这适用于你;)



所有在一起:

 <?php // app / models / MedicineType.php 

class MedicineType扩展Eloquent {

//确定要使用的数据库表
protected $ table ='medicine_types';

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

}

和:

 <?php // app / models / User.php 

class用户扩展Eloquent {

//确定要使用的数据库表
protected $ table ='users';

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

}

测试是否有效: p>

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

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



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


I have 2 models in my app, 'User' & 'MedicineType' (each User belongs to one MedicineType).

I made the one-to-many relation between two model using belongsTo() and hasMany(). hasMany() relation works perfectly but belongTo() doesn't work. Does anyone know where did I make a mistake?

User::find(1)->medicine_type [this returns nothing]

MedicineType::find(1)->users [this returns users]

Here's the code to Models:

class MedicineType extends Eloquent {

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


class User extends Eloquent {

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

And here is my database structure:

users:
    id
    name
    medicine_type_id 

medicine_types:
    id
    name

解决方案

The reason your relation is not working is not because of the relations specified in the model, but because of the method naming in the User model and not specifying the foreign key.

Instead of:

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

Use:

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

I hope this works for you ;)

Everything together:

<?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');
   }

}

and:

<?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');
   }

}

Testing if it works:

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

This successfully returns the related medicine_type's name.

I hope this helps you further ;)

这篇关于Laravel属于不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆