示例:员工 class员工扩展模型{保护$ fillable = ['name'];公共功能emails(){返回$ this-> belongsToMany('App \ Email')->使用('App \ PivotModel');}} 电子邮件 类电子邮件扩展模型{保护$ fillable = ['用户名'];公共职能雇员(){返回$ this-> belongsToMany('App \ Employee')->使用('App \ PivotModel');}} PivotModel class EmailEmployee扩展了Pivot{公共函数AssignedBy(){返回$ this-> belongsTo('App \ Employee','assigned_by');}} 请确保在数据透视表模型而非模型上扩展数据透视表 现在您可以这样做: $ user-> emails()-> first()-> pivot-> assignedBy -> first()的原因是您有很多对很多,这意味着您将获得分配给用户的电子邮件的集合.您通常会遍历它们,但对于本示例,只需选择第一个即可.如果只需要列值而不是关系值,则添加-> withPivot('assigned_by'),这将允许您直接访问该值.如果您要审核分配的时间,那么如果数据透视表包含时间戳,则还可能需要添加-> withTimestamps(),以便您也可以访问它们.Version: Laravel 5.4I have 3 ModelsModel: Employee protected $fillable = ['name']; public function emails(){ return $this->belongsToMany('App\Email')->using('App\EmailEmployee'); }Model: Email protected $fillable = ['username']; public function employees(){ return $this->belongsToMany('App\Employee')->using('App\EmailEmployee'); }Every Employee has many email access and emails allocates to many employees. But I have another column in email_employee tableemail_id (emails table)employee_id (employees table)assigned_by (employees table)how to make relation of assigned_by column with employees tablePivot Model use \Illuminate\Database\Eloquent\Relations\Pivot; class EmailEmployee extends Pivot{ public function assignedBy(){ return $this->belongsTo('App\Employee'); } }I tried $email = Email::find(1); dd($email->employee[0]->pivot->assignedBy);But not working 解决方案 Custom Intermediate Table ModelTo solve your problem, you should look to use the ->using() method on the belongsToMany method.The subsection "Defining Custom Intermediate Table Models" in this link briefly describes this. eloquent-relationships#many-to-manyYou basically create a model for the pivot table so that you can define additional relations to it.You can still access data from Blade and Controllers the way you are now as Laravel will still deal with the relationship for you. However, you can access the pivot table with ->pivot and as you have told laravel to use a model for the pivot table, you can also access all the relationship defined functions from that model.Example: Employeeclass Employee extends Model{protected $fillable = ['name']; public function emails(){ return $this->belongsToMany('App\Email') ->using('App\PivotModel'); }} Email class Email extends Model { protected $fillable = ['username']; public function employees(){ return $this->belongsToMany('App\Employee') ->using('App\PivotModel'); } } PivotModelclass EmailEmployee extends Pivot{ public function assignedBy(){ return $this->belongsTo('App\Employee','assigned_by'); }}Be Sure to extend Pivot on the pivot model and not ModelNow you can just do:$user->emails()->first()->pivot->assignedByThe reason for the ->first() is that you have a many to many, meaning that you will be getting a collection of emails assigned to the user. You would normally loop through them but for this example, simply selecting the first will do the same.If you just want the column value and not the relationship value, then add ->withPivot('assigned_by') which will allow you to access the value directly.If you are wanting to audit when the assignment was made, then you may also want to add ->withTimestamps() if your pivot table has timestamps included, so that you can access those too. 这篇关于如何在Laravel中的数据透视表中添加其他列关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!