【问题标题】:Laravel Cities relation foreign keyLaravel 城市关系外键
【发布时间】:2013-01-15 10:29:49
【问题描述】:

我真的是 laravel 新手,我确定我做错了什么

我有 2 张桌子。 Citiesusers_metadata

我的城市表如下所示

id | City        |
1  | New York    |
1  | Los Angeles |

用户元数据

user_id | firt name | last name | cities_id |
1       | Jonh      | Doe       |         2 |

所以我的问题是,当我创建一个关系时,我得到了纽约,因为城市 ID 与用户 ID 匹配

城市模型

class City extends Eloquent 
{
    public static $table = "cities";

    public function profile()
    {
        return static::has_many('Profile');
    }
}   

个人资料模型

class Profile extends Eloquent 
{
    public static $timestamps = false;

    public static $table = "users_metadata";

    public static $key = "user_id";

    public function city()
    {
        return static::has_one('City');
    }
}   

错误

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'profile_id' in 'where clause'

SQL: SELECT * FROM `cities` WHERE `profile_id` = ? LIMIT 1

Bindings: array (
  0 => 1,
)

如果我没有将 id 传递给有一个,我会收到以下错误

好的,我明白了。

所以我的问题是我是否能够以某种方式传递外键 cities_id 以匹配我的关系?还是我做错了?谁能给我一个基本的例子?

谢谢大家

【问题讨论】:

  • @w0rldart 如果原因不明显,请说明您的编辑原因 - 并且不需要“编辑”标题。

标签: php orm laravel relation


【解决方案1】:

试试这个:

城市模型

class City extends Eloquent 
{
    public static $table = "cities";
    public function profile()
}   

轮廓模型

class Profile extends Eloquent 
{
    public static $timestamps = false;
    public static $table = "users_metadata";
    public static $key = "user_id";
    public function city()
    {
        return static::belongs_to('City');
    }
}

我遇到了同样的问题,我认为我应该使用 has_one,但我需要使用 belongs_to。 user1808639 可能不起作用,因为您在城市模型中仍然有一个“has_many”。

【讨论】:

    【解决方案2】:

    试试这个:

    class Profile extends Eloquent
    {
        public function city()
        {
            return $this->belongs_to('City'); // city_id in the table
        }
    }
    

    【讨论】:

      【解决方案3】:

      Laravel 有一种自动检测外键的方法。所以对于你的数据库模式......这个模型应该可以正常工作 试试下面的

      user_id | firt name | last name |   city_id | //city not cities
      1       | Jonh      | Doe       |         2 |
      

      -

      class Profile extends Eloquent 
      {
          public static $timestamps = false;
          public static $table = "users_metadata";
      
          public function city()
          {
              return $this->has_one('City');
          }
      }
      

      /

      class City extends Eloquent 
      {
          public static $timestamps = false;
      
          public function profiles()
          {
              return $this->has_many('Profile');
          }
      }
      

      【讨论】:

      • 感谢您的回复,但还是一样,我找回了 SQLSTATE[42S22]: Column not found: 1054 Unknown column 'profile_id' in 'where Clause',我仍然需要将 id 传递给城市关系,并且仍然将 id 与 user_id 而不是 id 与 city_id SQL 匹配: SELECT * FROM cities WHERE profile_id = ?限制 1 个
      • 只是一个小错字修复,profiles() 不是profile(),因为它是has_many。在调用模型的地方发布代码
      • 我强烈建议你阅读laravel.com/docs/database/eloquent,因为public static $table = "cities"; 的行完全没有用,因为 Laravel 会自动这样做。你还有其他users 表吗?如果没有,请将该表中的users_id 设置为id 并将控制器发布到您调用模型的位置......不仅是模型
      【解决方案4】:

      表格行不应包含空格。所以重命名“原名”| “名字”中的“姓氏” | “姓”。 SQL 错误告诉我你正在以相反的方式调用模型,例如 City::find(1) 在这种情况下 Laravel 期望在“Cities”表中的配置文件的外键通常是“profile_id”。

      我猜你正在寻找这样的东西:

      $user_id = 1; 
      $user = Profile::find($user_id);
      
      echo $user->firstname." ".$user->lastname." lives in ".$user->city->city;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-07-20
        • 2019-03-27
        • 2016-12-22
        • 1970-01-01
        • 1970-01-01
        • 2021-07-26
        • 2017-12-26
        相关资源
        最近更新 更多