Use of Foreign Keys Laravel 5.6

Asked

Viewed 250 times

1

Good afternoon to you all. I created a Migration "clients" in the Newsletter that receives 3 foreign keys from the entities: modalities, requests and locals.

In my view when registering a client and viewing in phpmyadmin I see that all data is saved with their respective names, but in this table clients, the columns modes, orders and locals are saved only the id.

For example:

In the mode select the user chooses the first select item which is Special.

In the order select the user chooses the second select item which is Varied.

In the locals select the user chooses the third select item which is São Paulo.

If I go there in phpmyadmin to see how it was in the register will be in these columns respectively: 1, 2 and 3.

Is it so or am I doing something wrong so the name is not appearing?

My customers are like this:

$table->increments('id');
$table->string('nome_cliente',80);
$table->string('email_cliente',80)->unique();
$table->string('endereco',50);
$table->string('situacao',20);
$table->integer('modalidade_id')->unsigned();
$table->foreign('modalidade_id')->references('id')->on('modalidades');
$table->integer('pedido_id')->unsigned();
$table->foreign('pedido_id')->references('id')->on('pedidos');
$table->integer('locals_id')->unsigned();
$table->foreign('locals_id')->references('id')->on('locals');

            $table->softDeletes();

            $table->timestamps();
  • That’s correct, relationships should be made only with ID even, when you are listing the orders and in this listing display the user name is only you do an INNER JOIN.

  • Okay, so in my bank you’ll be showing up the ID instead of the name?

  • That’s right, man.

1 answer

0

Foreign key (Foreign key) is the field that establishes the relationship between two tables.

Thus, a column corresponds to the same column as the primary key of another table.

Thus, it should be specified in the table that contains the foreign key which are these columns and to which table is related.

The database will check whether all fields referencing the table are specified, and by default the vast majority of Databases by convention use the "id" (identifiers) of each table.

Makes it easier to compare one int, than a string, don’t you think? Because a string can contain variations, between minuscules, uppercase and sizes, an int, will always be an int.

Browser other questions tagged

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