【发布时间】:2018-07-10 14:05:48
【问题描述】:
我有两个表:用户和个人资料。 一个用户可能有一个或多个配置文件。我想访问两个表的组合数据:用户表中的“ID”是配置文件表中的外键
用户模型:
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'first_name','last_name', 'email', 'password','phone','user_type',
];
protected $hidden = [
'password', 'remember_token',
];
public function profile()
{
return $this->hasMany(profile::class);
}
}
轮廓模型是:
class profile extends Model
{
protected $fillable = [
'id','relationship_status','dob','height',
'weight','primary_language'
];
protected $primaryKey = 'profile_id';
public function User()
{
return $this->belongsTo(User::class,'id','id');
}
}
【问题讨论】:
-
您能否通过示例详细说明您想要什么
-
用户
hasMany个人资料或hasOne个人资料? -
从你的问题我发现你的数据库结构 id 不正确。个人资料表中的 user_id 在哪里?
-
我在配置文件表中使用了相同的字段名称,ID
标签: laravel laravel-5 eloquent