如何连接数据库中的3个表?

How to connect 3 tables in the database?

问题是我有 3 个数据库 tables

我想在单击特定用户后使用 attribute_id 从第二个 table 获取 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