Laravel 5.2 如何从一个关系中获取两个或多个 table 的值
Laravel 5.2 How to get values from two or more table from a relationship
我有一个默认身份验证 table 用户和另一个 table user_profiles
user(id,email,password)
user_profiles(id,first_name,last_name,mobile)
我正在使用多对多关系连接这两个 table
为此,我在两个模型 class- User 和 UserProfile
中添加了关系
// User Model
public function userProfiles()
{
return $this->belongsToMany('App\Models\UserProfile', 'user_user_profile',
'user_profile_id', 'user_id');
}
//UserProfile Model
public function users()
{
return $this->belongsToMany('App\User', 'user_user_profile',
'user_profile_id', 'user_id');
}
并且我尝试通过用户 table 使用
访问 UserProfle 详细信息
$user=\App\User::find(1)->userProfiles()->get();
但没有用,也试过了
/$user = \App\User::findOrFail(1)->with('userProfiles')->get();
这也不起作用,请帮助
- 获取 user_profile table 详细信息以及用户 table
- 如何访问数据透视表 table(user_id,user_profile_id) 值
- 如何将来自多个 table 的这些数据显示到一个视图表单中?
您在用户模型中定义了错误的关系:交换 user_id
和 user_profile_id
// User Model
public function userProfiles()
{
return $this->belongsToMany('App\Models\UserProfile', 'user_user_profile',
'user_id' , 'user_profile_id');
}
我有一个默认身份验证 table 用户和另一个 table user_profiles
user(id,email,password)
user_profiles(id,first_name,last_name,mobile)
我正在使用多对多关系连接这两个 table 为此,我在两个模型 class- User 和 UserProfile
中添加了关系 // User Model
public function userProfiles()
{
return $this->belongsToMany('App\Models\UserProfile', 'user_user_profile',
'user_profile_id', 'user_id');
}
//UserProfile Model
public function users()
{
return $this->belongsToMany('App\User', 'user_user_profile',
'user_profile_id', 'user_id');
}
并且我尝试通过用户 table 使用
访问 UserProfle 详细信息 $user=\App\User::find(1)->userProfiles()->get();
但没有用,也试过了
/$user = \App\User::findOrFail(1)->with('userProfiles')->get();
这也不起作用,请帮助
- 获取 user_profile table 详细信息以及用户 table
- 如何访问数据透视表 table(user_id,user_profile_id) 值
- 如何将来自多个 table 的这些数据显示到一个视图表单中?
您在用户模型中定义了错误的关系:交换 user_id
和 user_profile_id
// User Model
public function userProfiles()
{
return $this->belongsToMany('App\Models\UserProfile', 'user_user_profile',
'user_id' , 'user_profile_id');
}