Hello friends, I'm working on an api rest in laravel 5.4 and so far I need to make a relationship between two tables, creating an intermediate one. I'm just not sure if I'm doing it right and I wanted your opinion and advice: for example table roles:
class CreateRolesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('denominacion');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('roles');
}
}
table modules:
class CreateModulesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('modules', function (Blueprint $table) {
$table->increments('id');
$table->string('denominacion');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('modules');
}
}
and the intermediate table:
class CreateRolesModulesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('roles_modules', function (Blueprint $table) {
$table->increments('id');
$table->integer('roles_id')->index();
$table->integer('modules_id')->index();
// $table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('roles_modules');
}
}
and the relationship in the models, role model: use Illuminate \ Database \ Eloquent \ Model; use App \ modules;
class roles extends Model
{
public function moduls()
{
return $this->belongsToMany('modules');
}
}
Model module:
use Illuminate\Database\Eloquent\Model;
use App\roles;
class modules extends Model
{
//
public function rols(){
return $this->belongToMany('roles');
}
}
Please tell me, that's fine or how I should do it