Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

PeterBre's avatar

Dropdown for two foreign keys depend on one table.

For example i have three tables with an Index: $table->index(["table2_id", "table2_table1_id"], 'fk_table3_table21_idx'); in table 3.

Tables:

Table name: table1  Migration File: 2020_03_02_000000_create_table1_table.php

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTable1Table extends Migration
{
    /**
     * Schema table name to migrate
     * @var string
     */
    public $tableName = 'table1';

    /**
     * Run the migrations.
     * @table table1
     *
     * @return void
     */
    public function up()
    {
        Schema::create($this->tableName, function (Blueprint $table) {
            $table->bigIncrements('idtable1');
            $table->string('table1col', 45)->nullable();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
     public function down()
     {
       Schema::dropIfExists($this->tableName);
     }
}



Table name: table2  Migration File: 2020_03_02_000000_create_table2_table.php

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTable2Table extends Migration
{
    /**
     * Schema table name to migrate
     * @var string
     */
    public $tableName = 'table2';

    /**
     * Run the migrations.
     * @table table2
     *
     * @return void
     */
    public function up()
    {
        Schema::create($this->tableName, function (Blueprint $table) {
            $table->bigIncrements('idtable2');
            $table->string('table2col', 45);
            $table->unsignedBigInteger('table1_id');

            $table->index(["table1_id"], 'fk_table2_table1_idx');


            $table->foreign('table1_id', 'fk_table2_table1_idx')
                ->references('idtable1')->on('table1')
                ->onDelete('no action')
                ->onUpdate('no action');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
     public function down()
     {
       Schema::dropIfExists($this->tableName);
     }
}



Table name: table3  Migration File: 2020_03_02_000000_create_table3_table.php

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTable3Table extends Migration
{
    /**
     * Schema table name to migrate
     * @var string
     */
    public $tableName = 'table3';

    /**
     * Run the migrations.
     * @table table3
     *
     * @return void
     */
    public function up()
    {
        Schema::create($this->tableName, function (Blueprint $table) {
            $table->bigIncrements('idtable3');
            $table->string('table3col', 45);
            $table->unsignedBigInteger('table2_id');
            $table->unsignedBigInteger('table2_table1_id');

            $table->index(["table2_id", "table2_table1_id"], 'fk_table3_table21_idx');


            $table->foreign('table2_id', 'fk_table3_table21_idx')
                ->references('idtable2')->on('table2')
                ->onDelete('no action')
                ->onUpdate('no action');
        });
    }

   
     public function down()
     {
       Schema::dropIfExists($this->tableName);
     }
}

Is it possible to ask for the two foreign key "table2_id", "table2_table1_id" in table 3 with one select dropdown like?:

<td><select name="fk_table3_table21_idx" class="form-control fk_table3_table21_idx">
<option value=""> -- Choose one. --</option>
@foreach($table2 as $t2)
<option value="{{$t2->idtable2, table1_id}}"></option>
@endforeach
</select></td>;
0 likes
1 reply
PeterBre's avatar

On which way can i retrieve the second value of the index table2_table1_id?

$table->index(["table2_id", "table2_table1_id"], 'fk_table3_table21_idx');

I didn`t find any example in the Documentation. https://laravel.com/docs/6.x

Please or to participate in this conversation.