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