Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rublopweb's avatar

Relationship between two models

Hi, i would like to use two related models in my project, "payrolls" and "table_prices". Currently, i am using a one to many relationship, a table_price has many payrolls and a payroll belongs to a single table_price. The payrolls will be generated every month based on the table price, the problem is that the table_price can start to be valid not at the beginning of the month.

For example a table_price can start to be valid at this date "15-11-2023", so the payroll of November should be calculated considering that the first half of the month has one table price and the second half has another. So, in this case the payroll will have two table_prices. But the general case, will always be that the payroll will be calculated with only one table_price.

Should i use a many to many relationship just to handle this possibility of having two table_prices in one payroll or it is a better way to handle this case?

Thanks a lot,

0 likes
1 reply
LaryAI's avatar
Level 58

In this case, using a many-to-many relationship might be overkill since the general case is that an invoice will be calculated with only one table price. Instead, you can handle this scenario by introducing a pivot table to store the relationship between invoices and table prices.

Here's how you can set it up:

  1. Create a new migration to create the pivot table:
php artisan make:migration create_invoice_table_price_table --create=invoice_table_price
  1. In the generated migration file, define the schema for the pivot table:
public function up()
{
    Schema::create('invoice_table_price', function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteger('invoice_id');
        $table->unsignedBigInteger('table_price_id');
        $table->timestamps();

        $table->foreign('invoice_id')->references('id')->on('invoices')->onDelete('cascade');
        $table->foreign('table_price_id')->references('id')->on('table_prices')->onDelete('cascade');
    });
}
  1. Run the migration to create the pivot table:
php artisan migrate
  1. Update your models to define the relationship:

In the Invoice model:

public function tablePrices()
{
    return $this->belongsToMany(TablePrice::class);
}

In the TablePrice model:

public function invoices()
{
    return $this->belongsToMany(Invoice::class);
}
  1. When creating an invoice, you can associate one or multiple table prices using the attach method:
$invoice = new Invoice;
$invoice->save();

$invoice->tablePrices()->attach($tablePrice1);
$invoice->tablePrices()->attach($tablePrice2);
  1. To retrieve the table prices associated with an invoice, you can use the tablePrices relationship:
$invoice = Invoice::find($invoiceId);
$tablePrices = $invoice->tablePrices;

By using a pivot table, you can handle the scenario where an invoice may have multiple table prices.

Please or to participate in this conversation.