Laravel 4雄辩的ORM通过动态属性访问一对一的关系 [英] Laravel 4 Eloquent ORM accessing one-to-one relationship through dynamic properties

查看:91
本文介绍了Laravel 4雄辩的ORM通过动态属性访问一对一的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一个非常简单的关系,位于用户表和User_profiles表之间。每个用户都有一个user_profile,因此它是一个简单的一对一。根据 http://four.laravel.com/docs/eloquent#one-to-一个我已经添加了以下功能给我的用户模型:

  public function user_profile()
{
return $ this-> hasOne('User_profile');
}

这是我的User_profile模型中定义的关系:

  public function user()
{
return $ this-> belongsTo('User');
}

我正在尝试从控制器访问:

  //获取当前用户
$ user = User :: find(Auth :: user() - > id);
$ profile = $ user-> user_profile;

print_r($ user);
print_r($ profile);

echo用户名是:。 $ user-> user_profile-> first_name。 。 $用户> user_profile->姓氏;

不幸的是,打印$ user打印出用户模型字段很好,但不显示任何痕迹关系; $个人资料为空。 关系数组也是空的,我猜想应该填写。



我正在尝试使用这里建议的动态属性 http://four.laravel.com/docs/eloquent#dynamic-properties



否则,如果我只是去:

  echo用户名是:。 $ user-> user_profile() - > first() - > first_name。 。 $用户> user_profile() - >首先() - >姓氏; 

它的工作原理..但我不太喜欢这样做。



任何建议?

解决方案

好的,所以问题与使用下划线有关类名。 Laravel遵循PSR 0和1,如此处所述。



这意味着我必须命名我的模型类和文件名UserProfile(即使我的MySql表命名为'user_profiles'),然后更新我的用户模型以具有一个名为userProfile()的功能。 / p>

一旦我更新了命名,我可以通过执行以下操作来自动访问关系:

 code> $ user = Auth :: user(); 
echo $ user-> userProfile-> first_name;


I am trying to model a pretty straightforward relationship, between the 'Users' table and the 'User_profiles' table. Each user has a user_profile, so it's a simple one-to-one. As per the docs found @ http://four.laravel.com/docs/eloquent#one-to-one I have added the following function to my User model:

public function user_profile()
{
    return $this->hasOne('User_profile');
}

and this is the relationship defined in my User_profile model:

public function user()
{
    return $this->belongsTo('User');
}

I am trying to access from the controller like this:

    // 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;

Unfortunately printing $user prints out the User model fields just fine, but doesn't show any trace of a relationship; $profile is empty. The 'relations' array is also empty, which I'm guessing should maybe be filled.

I am trying to use the 'dynamic properties' as suggested here http://four.laravel.com/docs/eloquent#dynamic-properties

Otherwise if I just go:

echo "User's name is: " . $user->user_profile()->first()->first_name . ' ' . $user->user_profile()->first()->last_name;

It works.. but I don't really like having to do that.

Any suggestions?

解决方案

Ok so the problem had to do with using underscores in the class names. Laravel follows PSR 0 and 1 as outlined here.

What this means is I had to name my model class and filename UserProfile (even though my MySql table named remained 'user_profiles'), and then update my User model to have a function called userProfile().

Once I updated the naming, I could access the relationships automatically by doing something like:

$user = Auth::user();
echo $user->userProfile->first_name;

这篇关于Laravel 4雄辩的ORM通过动态属性访问一对一的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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