How to name table pivot Many-to-Many

Asked

Viewed 981 times

1

How to name pivot tables in Laravel, so that it is not necessary to inform its name in the relationship method (belongsToMany)?

In the documentation I found this reference:

Many-to-Many Relations are Slightly more Complicated than hasOne and hasMany relationships. An example of such a Relationship is a user with Many roles, Where the roles are also Shared by other users. For example, Many users may have the role of "Admin". To define this Relationship, three database Tables are needed: users, roles, and role_user. The role_user table is derived from the Alphabetical order of the Related model Names, and contains the user_id and role_id Columns.

...

In short, it is informed that I can take the name of the two models involved and create a nomenclature representing the alphabetic junction of these, ie if I have a table product and another user, would be producto_usuario, with that would not need to pass the name of the pivot in the method belongsToMany()

The question is, would the alphabetical order of the models or Migrations?

1 answer

2


The question is, would the alphabetical order of the models or Migrations?

At the end of the translation it says: translating Google Translator: The role_user table is derived from the alphabetical order of the related model names and contains the columns user_id and role_id.

That is to say, the name of the intermediate table starts in alphabetical order and R is before U, respectively role and user and generated the table role_user.

Another example to clarify:

If in your project two tables are created with the name of autor and livro the name of the intermediate table would be autor_livro and the fields autor_id and livro_id and the migration as an example:

Migration author

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class Autor extends Migration
{
    public function up()
    {
        Schema::create('autor', function (Blueprint $table) {
            $table->increments('id');            
            $table->string('name', 100)->index();            
            $table->timestamps();
        });
    }   
    public function down()
    {
        Schema::dropIfExists('autor');
    }
}

Migration book

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class Livro extends Migration
{    
    public function up()
    {
        Schema::create('livro', function (Blueprint $table) {
            $table->increments('id');            
            $table->string('title', 100)->index();            
            $table->timestamps();
        });
    }
    public function down()
    {
        Schema::dropIfExists('livro');
    }
}

Migration self-published

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AutorLivro extends Migration
{

    public function up()
    {
        Schema::create('autor_livro', function (Blueprint $table) {
            $table->integer('autor_id')->unsigned();            
            $table->integer('livro_id')->unsigned();            
            $table->primary(['autor_id', 'livro_id']);
            $table->timestamps();

            $table->foreign('autor_id')
                ->references('id')
                ->on('autor');

            $table->foreign('livro_id')
                ->references('id')
                ->on('livro');

        });
    }


    public function down()
    {
        Schema::dropIfExists('autor_livro');
    }
}

and consequently their models:

Model Author

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Autor extends Model
{
    protected $table = 'autor';
    protected $dates = ['created_at','updated_at'];
    protected $fillable = ['name'];    
    public $timestamps = true;

    public function livros()
    {
        return $this->belongsToMany(Livro::class);
    }
}

Model Book

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Livro extends Model
{
    protected $table = 'livro';
    protected $dates = ['created_at','updated_at'];
    protected $fillable = ['title'];    
    public $timestamps = true;

    public function autores()
    {
        return $this->belongsToMany(Autor::class);
    }
}

but, remember that the first configuration is mandatory the others followed the standard nomenclature described in the documentation.

There is already another example here, but, unlike this detail how to configure without the standard nomenclature described in the documentation and other examples

Browser other questions tagged

You are not signed in. Login or sign up in order to post.