【发布时间】:2014-08-21 15:52:03
【问题描述】:
我想创建简单的关系,以便使用User 模型在数据库中查找用户的个人资料信息。
在 User.php 我有:
class User extends Eloquent implements UserInterface, RemindableInterface {
...
public function profile() {
return $this->hasOne('UserProfile');
}
...
}
在model/UserProfile.php中:
class UserProfile extends Eloquent{
protected $table='user_profile';
public function user() {
return $this->belongsTo('User');
}
}
在数据库中users.user_id 和user_profile.user_id 是1000。
我想在控制器中使用此行访问用户配置文件:
public function getIndex()
{
$profile = $user->profile;
dd( $profile );
die;
}
dd() 的结果是:
NULL
我的人际关系问题是什么?
【问题讨论】:
-
数据库中是否存在 user_profile 行?
-
你的
getIndex()从哪里得到它的$user? -
@Unnawut 在 ProfileController 中访问用户配置文件
-
我假设
var_dump($user->user_id)给你正确的用户ID 对吧?你在profile()中尝试过$this->hasOne('UserProfile', 'user_id', 'user_id')吗?
标签: php mysql laravel orm laravel-4