Many-to-many relationship in laravel 5.4

1

I am doing the assignment of Teachers - Degrees, that is to say I have a Teaching model and a Grade model and their relationship is of many to many so when it comes to making the migration creates a pivot teacher_graduate table. According to I understand laravel does not use a model in the pivot table. The issue is that to perform this assignment I have to show in the index.blade.php the pivot table (records of the same) and the truth is not how to do it, that is, I do not know how to reference it. In the same way I need to save the assignments in the pivot table.

Teaching Model

Grade Model.

Teacher table migration.

class CreateDocenteTable extends Migration
{
    public function up()
    {
        Schema::create('docentes', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('nip')->unique();
        $table->string('nombres', 100);
        $table->string('apellidos', 100);
        $table->string('dui', 12)->unique();
        $table->string('nit', 20)->unique();
        $table->string('especialidad', 50);

        $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('docentes');
    }
}

Table migration Grade.

class CreateGradoTable extends Migration
{
    public function up()
    {
        Schema::create('grados', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('idturno')->unsigned();
        $table->string('nombre', 60);
         $table->foreign('idturno')->references('id')->on('turnos')->onDelete('cascade');
       $table->timestamps();
        });

        Schema::create('docente_grado', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('iddocente')->unsigned();
        $table->integer('idgrado')->unsigned();
            $table->foreign('iddocente')->references('id')->on('docentes')->onDelete('cascade');
            $table->foreign('idgrado')->references('id')->on('grados')->onDelete('cascade');
            $table->timestamps();
            });

    public function down()
    {
        Schema::dropIfExists('docente_grado');
        Schema::dropIfExists('grados');
    }
}
    
asked by Gab098 25.08.2017 в 04:11
source

1 answer

0

I think you have an error in defining the keys of the relationship, you have the same in both models, in the Degree model you have to change the order, that is, first the grade key and then the teacher.

    
answered by 21.02.2018 в 11:13