How can I relate tables in Laravel with foreign keys?

3

I already related it to this:

public function up()
{

    Schema::create('personal', function (Blueprint $table) {
       $table->increments('id_personal');
        $table->string('paterno',50);
        $table->string('materno',50);
        $table->string('nombre',50);
        $table->unsignedInteger('id_categoria');
        $table->foreign('id_categoria')->references('id_categoria')->on('categoria')
        $table->timestamps();
    });
}

But I want to relate the three tables also

    
asked by Carlos 23.08.2018 в 17:59
source

1 answer

2

I told you that the order of your migrations should be as follows

category

You generate the migration with the following command

php artisan make:migration create_categoria_table
public function up() {
    Schema::create('categoria', function(Blueprint $table){
        $table->increments('id_categoria');
        $table->decimal('costo_hora', 10, 2);
        $table->timestamps();
    });
}

personal

You generate the migration with the following command

php artisan make:migration create_personal_table
public function up() {
    Schema::create('personal', function (Blueprint $table) {
        $table->increments('id_personal');
        $table->string('paterno',50);
        $table->string('materno',50);
        $table->string('nombre',50);
        $table->unsignedInteger('categoria_id');
        $table->foreign('categoria_id')->references('id_categoria')->on('categoria')->onDelete('cascade');
        $table->timestamps();
    });
}

salary

You generate the migration with the following command

php artisan make:migration create_sueldo_table
public function up() {
    Schema::create('sueldo', function(Blueprint $table){
        $table->increments('id_sueldo');
        $table->decimal('horas', 10, 2);
        $table->decimal('importe', 10, 2);
        $table->string('periodo');
        $table->unsignedInteger('personal_id');
        $table->foreign('personal_id')->references('id_personal')->on('personal')->onDelete('cascade');
        $table->timestamps();
    });
}

CLARIFICATIONS

  
  • Migrations go one per file
  •   
  • The category table goes first, because it does not depend on any other to exist
  •   
  • staff goes after because it depends on its category structure
  •   
  • Salary goes to the end because its very existence depends on personal and at the same time personal depends on category
  •   

    OBSERVATIONS

    Foreign keys, try to name them in the following way

    nombreColumna_id
    
    user_id
    

    also in the singular

        
    answered by 23.08.2018 / 18:18
    source