Ad

Any Advantages Of Adding An Id Column To A Pivot Table In Laravel?

- 1 answer

Is there any advantages of having an id column in a pivot table (many to many relationship) in Laravel (i'm using version 5.1)?

With an id

        $table->increments('id');

        $table->integer('appointment_id')->unsigned();
        $table->foreign('appointment_id')->references('id')->on('appointments')->onDelete('cascade');

        $table->integer('client_id')->unsigned();
        $table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');

        $table->timestamps();

Without an id

        $table->integer('appointment_id')->unsigned();
        $table->foreign('appointment_id')->references('id')->on('appointments')->onDelete('cascade');

        $table->integer('client_id')->unsigned();
        $table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');

        $table->timestamps();
Ad

Answer

In general, the answer is no, provided that Laravel's Eloquent models are managing the relationship.

If, however, you need to access the tables from outside of Eloquent models (say, from another application or in the distant future when you rewrite your application to use the next big framework), an ID will come in handy.

Ad
source: stackoverflow.com
Ad