Relationship between tables Aravel 5.5

Asked

Viewed 51 times

0

I would like a suggestion on how to implement relation between tables in the 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();
    });
}
  • What specifically do you want? It’s not very clear. Edit the question and add more information.

  • id_professor so that if you already have id? You intend to have two primary keys for the same table?

  • I wanted to ask how I can relate the tables in the id_curso lines, id_professor.

  • Edit the question and put this information, so I could answer for you.

1 answer

0


Hello

I think this is what you want, see Blueprint’s Foreign() method

Check the documentation on https://laravel.com/docs/5.6/migrations#Foreign-key-constraints

 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();
    });
}
  • Yes, I just looked at the documentation. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.