【发布时间】:2013-01-15 10:29:49
【问题描述】:
我真的是 laravel 新手,我确定我做错了什么
我有 2 张桌子。 Cities 和users_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 如果原因不明显,请说明您的编辑原因 - 并且不需要“编辑”标题。