表之间的关系不起作用

问题描述 投票:0回答:1

我有两个表userprofile。概要表具有user_id作为外键。我已经关联了两个表。

在修补程序中,我可以看到建立了这种关系,但是在代码中,它没有从其他表中获取详细信息。

我也尝试过

return $this->belongsTo('User::class');

用户模型

public function profile() {
    return $this->hasOne('Profile');
}

个人资料模型

public function user() {
    return $this->belongsTo('User');
}
laravel eloquent
1个回答
0
投票

我看到两件事并不完全正确。

如果要关联:: class,则应使用:

return $this->belongsTo(User::class);

如果要使用字符串关联,请使用:

return $this->belongsTo('App\User');
return $this->hasOne('App\Profile');

另一个提示是,每次更换控制器时,您都必须再次关闭打开的修补匠

© www.soinside.com 2019 - 2024. All rights reserved.