Ich habe eine Viele-zu-Viele-Beziehung mit der folgenden Struktur:
|Table receipts | |- id | |- code | |- date | ----------------------- |Table plans | |- id | |- number | |- name | ----------------------- |Table plan_receipt |(Pivot) |- id | |- receipt_id | |- plan_id | |- employee_id | ----------------------- |Table employees | |- id | |- name | -----------------------
Wie Sie sehen, habe ich das typische many-to-many
关系,生成一个包含所述表的键的数据透视表,但我还有第三个 foreign key
引用另一个表 "employees"
,如何我可以将这张员工表与我的表关联起来吗?枢?除了使用 ->using()
außer dem Versuch, ein Modell für eine Pivot-Tabelle zu erstellen und Beziehungen herzustellen, aber bisher hat es bei mir nicht funktioniert, ich überlasse Ihnen ein Beispiel meines aktuellen Modells.
class Receipt extends Model { public function plans() { return $this->belongsToMany(Plan::class)->using(PlanReceipt::class); } } class Plan extends Model { public function receipts() { return $this->belongsToMany(Receipt::class); } } class PlanReceipt extends Pivot { protected $table = 'plan_receipt'; public function employee() { return $this->belongsTo(Employee::class, 'employee_id'); } } class Employee extends Model { public function plan_receipt() { return $this->hasMany(PlanReceipt::class, 'id'); } }
我猜您需要进行以下两项更改