如何连接数据库中的3个表?
How to connect 3 tables in the database?
问题是我有 3 个数据库 tables
- 第 table 位用户 user_id
- 第二个 table 是 attribute_id 和 attribute_name
的属性
- 第三个是评级 table 有 user_id 和 attribute_id
我想在单击特定用户后使用 attribute_id 从第二个 table 获取 attribute_name?可能吗?如果答案是肯定的,那么请给我一个建议,你是怎么做到的?提前谢谢先生?
- 我在这里上传图片,在我点击特定用户进行编辑后显示其来自用户 table 的记录和来自属性 table 的 attribute_id 使用多对多关系和问题是我们使用 attribute_id 从 Rating table 中获得特定 id 的 attribute_name?可能吗?
它的编辑表单和下面的提交按钮从属性 table 获取 ID,但我们想显示 attribute_name 来自评级 table
用户模型:
public function rating()
{
return $this->belongsTo(Rating::class);
}
评分模型:
public function attribute()
{
return $this->hasMany(Attribute::class, 'attribute_id');
}
然后在controller中调用:
dd(Users::with('rating.attribute')->where('user_id', 1));
查询::
$records = DB::table('attributes')->join('ratings','attributes.id','=','ratings.attribute_id')->where('user_id',$id)->get();
return view('employee.edit',compact('records'));
您的编辑视图页面
@foreach($records as $record)
<h4>Attributes ==>{{$record->attribute}}</h4>
@endforeach
问题是我有 3 个数据库 tables
- 第 table 位用户 user_id
- 第二个 table 是 attribute_id 和 attribute_name 的属性
- 第三个是评级 table 有 user_id 和 attribute_id
我想在单击特定用户后使用 attribute_id 从第二个 table 获取 attribute_name?可能吗?如果答案是肯定的,那么请给我一个建议,你是怎么做到的?提前谢谢先生?
- 我在这里上传图片,在我点击特定用户进行编辑后显示其来自用户 table 的记录和来自属性 table 的 attribute_id 使用多对多关系和问题是我们使用 attribute_id 从 Rating table 中获得特定 id 的 attribute_name?可能吗?
它的编辑表单和下面的提交按钮从属性 table 获取 ID,但我们想显示 attribute_name 来自评级 table
用户模型:
public function rating()
{
return $this->belongsTo(Rating::class);
}
评分模型:
public function attribute()
{
return $this->hasMany(Attribute::class, 'attribute_id');
}
然后在controller中调用:
dd(Users::with('rating.attribute')->where('user_id', 1));
查询::
$records = DB::table('attributes')->join('ratings','attributes.id','=','ratings.attribute_id')->where('user_id',$id)->get();
return view('employee.edit',compact('records'));
您的编辑视图页面
@foreach($records as $record)
<h4>Attributes ==>{{$record->attribute}}</h4>
@endforeach