简体   繁体   English

Laravel关系返回null

[英]Laravel relationship is returning null

i want to create simple Relationships for find user's profile information in database with User model. 我想创建简单的关系,以User模型在数据库中查找用户的个人资料信息。

in User.php i have: 在User.php中,我有:

class User extends Eloquent implements UserInterface, RemindableInterface {
    ...
    public function profile() {
        return $this->hasOne('UserProfile');
    }
    ...
}

and in model/UserProfile.php: 并在model / UserProfile.php中:

class UserProfile extends Eloquent{

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

in database users.user_id and user_profile.user_id is 1000 . 数据库中的users.user_iduser_profile.user_id1000

I want to access to user profile with this line in controller: 我想通过控制器中的此行访问用户个人资料:

public function getIndex()
{

    $profile = $user->profile;
    dd( $profile );
    die;
}

Result of dd() is : dd()结果是:

NULL

whats my Relationship problem? 我的人际关系问题是什么?

looks like the relationships aren't loaded. 好像没有加载关系。

either try loading them by using eager-loading 要么尝试使用eager-loading加载它们

User::with('user_profile')->get();

or load them later with lazy-loading: 或稍后通过延迟加载来加载它们:

$user = Auth::user();
$user->load('user_profile');

also since you're using another primary key property than id , you need to specify it (or in your relation declaration): 另外,由于您使用的是id以外的另一个主键属性,因此需要指定它(或在关系声明中):

protected $primaryKey = 'user_id';

For your users table you are using a different primary key other than id as expected so in your User model you should explicitly declare the protected $primaryKey = 'user_id' and also you may use this: 对于您的users表,您使用的是与预期不同的id以外的其他主键,因此在您的User模型中,您应明确声明protected $primaryKey = 'user_id' ,并且您也可以使用以下方法:

// Not tested with same foreign and local key
return $this->hasOne('UserProfile', 'user_id', 'user_id');

But it's better to keep the both keys different, probably you can rename the users.user_id to users.id . 但是最好将两个密钥保持不同,可能您可以将users.user_id重命名为users.id If you rename the users.user_id to users.id then everything will work without any changes at all. 如果重命名users.user_idusers.id那么一切都将不工作,在所有的任何更改。

I had suffered from the same issue and the reason was there was a whitespace after the column which would be used as relation. 我曾经遇到过同样的问题,原因是该列后面有一个空白,可以用作关系。 Simply deleting it solved. 只需删除即可解决。 May not be related to your question directly, but I believe this may save some hours of people who reach this page through a search engine. 可能与您的问题没有直接关系,但是我相信这可以节省一些通过搜索引擎访问此页面的人。

For me, the solution was eager loading just as @zwacky suggested: 对我来说,解决方案急于加载,就像@zwacky建议的那样:

or load them later with lazy-loading: 或稍后通过延迟加载来加载它们:

>$user = Auth::user();
>$user->load('user_profile');

This will do the solution when user_profile has just been created and you need to access user_profile through $user in another part of your code. 当刚刚创建了user_profile并且您需要在代码的另一部分中通过$user访问user_profile时,这将解决该问题。 Looks like laravel does not update the parent model immediately. 看起来laravel不会立即更新父模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM