How to name pivot table Many-to-Many

1

How to name pivot tables in Laravel, so you do not need to name the table in the relationship method ( belongsToMany )?

In the documentation I found this reference:

  

Many-to-many relationships 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 advised that I can get the name of the two models involved and create a nomenclature representing the alphabetical join of these, that is, if I have a product table and another user >, would be user_product , so you would not need to pass the pivot name in the belongsToMany ()

The question is, would it be the alphabetical order of models or migrations?

    
asked by anonymous 05.02.2018 / 23:04

1 answer

2
  

The question is, would it be the alphabetical order of models or migrations?

At the bottom of the translation says : Google Translate : < in> The role_user table is derived from the alphabetical order of the names of related models and contains the columns user_id and role_id.

That is, , the middle table name begins alphabetically with R before U , respectively role and user and the role_user table was generated.

Another example to clarify:

If your project creates two tables 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 migration as an example: / p>

  

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 author_book

<?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 keep in mind that the first setting is mandatory the rest followed the standard nomenclature described in the documentation .

Here's another example, but different from this detail how to configure it without the standard naming described in the documentation and other examples

06.02.2018 / 00:24