- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在“Users”表和“User_profiles”表之间建立一个非常简单的关系模型。每个用户都有一个 user_profile,因此是简单的一对一。根据文档发现@ http://four.laravel.com/docs/eloquent#one-to-one我已将以下函数添加到我的用户模型中:
public function user_profile()
{
return $this->hasOne('User_profile');
}
这是我的 User_profile 模型中定义的关系:
public function user()
{
return $this->belongsTo('User');
}
我正在尝试像这样从 Controller 访问:
// Get current user
$user = User::find(Auth::user()->id);
$profile = $user->user_profile;
print_r($user);
print_r($profile);
echo "User's name is: " . $user->user_profile->first_name . ' ' . $user->user_profile->last_name;
不幸的是,打印 $user 可以很好地打印出用户模型字段,但没有显示任何关系的痕迹; $配置文件为空。 “关系”数组也是空的,我猜它可能应该被填充。
我正在尝试使用此处建议的“动态属性”http://four.laravel.com/docs/eloquent#dynamic-properties
否则如果我直接走:
echo "User's name is: " . $user->user_profile()->first()->first_name . ' ' . $user->user_profile()->first()->last_name;
它有效..但我真的不喜欢这样做。
有什么建议吗?
最佳答案
好的,所以问题与在类名中使用下划线有关。 Laravel 遵循 PSR 0 和 1,如 here 所述.
这意味着我必须将我的模型类和文件名命名为 UserProfile (即使我的 MySql 表仍命名为“user_profiles”),然后更新我的 User 模型以具有名为 userProfile() 的函数。
更新命名后,我可以通过执行以下操作自动访问关系:
$user = Auth::user();
echo $user->userProfile->first_name;
关于Laravel 4 Eloquent ORM 通过动态属性访问一对一关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16888029/
我在使用一对一映射时遇到问题。我搜索了互联网并找到了许多解决方案,但没有一个令人满意。大多数示例都带有将父实例存储在子类中的开销。 我只想在具有外键约束关系的子类中使用父 ID,但不想在子类中保留任何
我有以下设置: Micronaut 3.x Java 15 我要更新的实体: @Setter @Getter @Entity @ToString @Table(name = "single_choic
我正在使用AVAudioPlayer制作MP3播放器。我有多种MP3声音,想一一播放。以下是我的应用程序的逻辑: ///// For playing 1st sound mp3Player = [[A
所以这就是问题所在。我有 2 个模型: 裁判级别和裁判 两个都有: class RefereeLevel(models.Model): level = models.PositiveSmall
我想将数组添加到列表或多维数组(不是一次全部...)。但是我真的不明白为什么这应该那么难。 可以说我有这个: string[] a = { "h", "b"}; string[] b
我有一个长度为 1000 的数字序列,我将其分成 100 个长度的序列。所以我最终得到了 901 个长度为 100 的序列。让前 900 个序列为 trainX。 trainY 是这些序列中的第 2
关键字:association 一对一映射(一个班级只有一个班主任) ?
所以,这是我第一次学习计算机语言。我选择了python和django。现在,我了解了 python 和 django 的许多基本概念。我可以使用 View 和所有其他内容创建新页面。但我仍然对这些关系
在一对一映射中,我编写了以下代码行。 @GenericGenerator(name = "generator", strategy = "foreign", parameters = @Paramet
我有两个如下所示的实体 @Data @EqualsAndHashCode(callSuper = true) @Entity @Table(name = "foo") @Audited @AuditO
我的问题很简单.. 假设有 2 个类..书籍和作者 假设一本书只能由一位作者撰写。 一个作家可以写很多本书。 假设作者有唯一的名字。 [两个作者不能同名] 现在..假设所有 hibernate\JPA
我正在尝试创建一个实体,如下所示 @Data public class Person { @Id private String id; @OneToMany(mapp
我有一个包含字段的Project表 ID PROJECT_BASELINE_ATTRIBUTES_ID (FK for table PROJECT_BASELINE_ATTR) 这个表有如下映射
我正在学习基本的 hibernate 教程。我正在尝试在 2 个表之间建立一对一的关系。当我尝试插入 Client 表时它起作用了,但是当我尝试做相反的事情时(插入 Facture 表)我得到了这个异
我已经在 hibernate 3 中使用注释完成了一对一映射,我有两张表“组”和“类别”。类别是预定义的。当用户选择类别和组时,CategoryId和goupid应该只插入组表中。 那么应该如何映射。
我使用Linux服务Fedora 4.14.33-51.37.amzn1.x86_64。我想使用 NAT 1 对 1。例如是否相同problem我的方案是:我的服务器有两个网络接口(interface
我正在尝试与实体 Revision 创建一对一、自引用、双向关系(哇),看起来像这个: /** * @Entity() * @Table(name="rev") */ class Revisio
我需要两个实体之间的链接,所以我使用一对一 @Entity @Table(name = "T_USER") public class User implements Serializable {
一对一: 一对一的关系极为一个数据仅对应一个数据,用下图的结构图可以帮助理解: 下面用代码实现一下,首先要创建工程项目如下: 接着,我们定义模型: 来到models.py文件,创建两
假设我有5列。 pd.DataFrame({ 'Column1': [1, 2, 3, 4, 5, 6, 7, 8, 9], 'Column2': [4, 3, 6, 8, 3, 4, 1, 4, 3
我是一名优秀的程序员,十分优秀!