I have a many-to-many relationship with the following structure:
|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 |
-----------------------
As you can see, I have the typical many-to-many relationship generating a pivot table that contains the keys of said tables, but I also have a third foreign key that refers to another table "employees", how can I relate this table of employees with my table? pivot? try to create a model for the pivot table and make the relationship, in addition to using ->using() but so far it has not worked for me, I leave you with an example of my current models.
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');
}
}