Relation between the tables laravel 5.5

0

I would like a suggestion on how to implement relationship between tables in laravel using migration.

 public function up()
{
    Schema::create('professors', function (Blueprint $table) {
        $table->increments('id');
        $table->string('id_professor');
        $table->string('nome');
        $table->string('data_nascimento');
        $table->timestamps();
    });
}

 public function up()
{
    Schema::create('courses', function (Blueprint $table) {
        $table->increments('id');
        $table->string('id_curso');
        $table->string('nome');
        $table->string('id_professor');
        $table->timestamps();
    });
}

public function up()
{
    Schema::create('students', function (Blueprint $table) {
        $table->increments('id');
        $table->string('id_aluno');
        $table->string('nome');
        $table->string('data_nascimento');
        $table->string('logradouro');
        $table->string('numero');
        $table->string('bairro');
        $table->string('cidade');
        $table->string('estado');
        $table->string('cep');
        $table->string('id_curso');
        $table->timestamps();
    });
}
    
asked by anonymous 11.07.2018 / 19:34

1 answer

0

Hello

I think this is what you want, see the foreign () method of Blueprint

Check the documentation at link

 public function up()
{
    Schema::create('professors', function (Blueprint $table) {
        $table->increments('id');
        $table->string('id_professor');
        $table->string('nome');
        $table->string('data_nascimento');
        $table->timestamps();
    });
}

 public function up()
{
    Schema::create('courses', function (Blueprint $table) {
        $table->increments('id');
        $table->string('id_curso');
        $table->string('nome');
        $table->string('id_professor');


        $table->foreign('id_professor')->references('id_professor')->on('professors');


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

public function up()
{
    Schema::create('students', function (Blueprint $table) {
        $table->increments('id');
        $table->string('id_aluno');
        $table->string('nome');
        $table->string('data_nascimento');
        $table->string('logradouro');
        $table->string('numero');
        $table->string('bairro');
        $table->string('cidade');
        $table->string('estado');
        $table->string('cep');
        $table->string('id_curso');


        $table->foreign('id_curso')->references('id_curso')->on('courses');


        $table->timestamps();
    });
}
    
12.07.2018 / 22:58