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