Intermediate Table and Laravel Relations 5.4

0

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

    
asked by j.dow 12.08.2017 в 16:48
source

1 answer

3

In the migration of the intermediate table add the foreign keys :

$table->foreign('roles_id')->references('id')->on('roles');
$table->foreign('modules_id')->references('id')->on('modules');

In the roles model:

return $this->belongsToMany(modules::class, 'roles_modules', 'id' /* de roles */, 'id' /* de modules */);

In the modules :

model
return $this->belongsToMany(roles::class, 'roles_modules', 'id' /* de modules */, 'id' /* de roles */);

If you wanted to get fields from the intermediate table it would be:

return $this->belongsToMany(...)->withPivot('nombreCampo');
  

As a tip, Laravel and many other 'modern' programming frameworks and languages apply the code writing style CamelCase .

    
answered by 12.08.2017 / 16:58
source