1

I'm trying to create a foreign key in the teachers table. after migration, all the columns were there, but the foreign key was not created

First, I have created two tables, which are the users table and the courses table.

if(! Schema::hasTable('users')) {
            Schema::create('users', function (Blueprint $table) {
                $table->increments('id');
                $table->string('name');
                $table->string('email');
                $table->string('password');
                $table->string('remember_token')->nullable();

                $table->timestamps();

            });
        }
if(! Schema::hasTable('courses')) {
            Schema::create('courses', function (Blueprint $table) {
                $table->increments('id');
                $table->string('title');
                $table->string('slug')->nullable();
                $table->text('description')->nullable();
                $table->decimal('price', 15, 2)->nullable();
                $table->string('course_image')->nullable();
                $table->date('start_date')->nullable();
                $table->tinyInteger('published')->nullable()->default(0);

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

                $table->index(['deleted_at']);
            });
        }

Then i created another table called 'teachers' with foreign keys

if(! Schema::hasTable('teacher')) {
            Schema::create('teacher', function (Blueprint $table) {
                $table->increments('id');
                $table->integer('user_id')->unsigned();
                $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
                $table->integer('course_id')->unsigned();
                $table->foreign('course_id')->references('id')->on('courses')->onDelete('cascade');
                $table->string('teachers_image')->nullable();
                $table->text('education')->nullable();
                $table->text('contact')->nullable();

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

            });
        }

after the migration i can see the tables was there but the foreign key was not created

aminography
  • 21,986
  • 13
  • 70
  • 74
Asha
  • 805
  • 3
  • 10
  • 18

1 Answers1

0

try this

if(! Schema::hasTable('teacher')) {
            Schema::create('teacher', function (Blueprint $table) {
                $table->increments('id');
                $table->unsignedInteger('user_id');
                $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
                $table->unsignedInteger('course_id');
                $table->foreign('course_id')->references('id')->on('courses')->onDelete('cascade');
                $table->string('teachers_image')->nullable();
                $table->text('education')->nullable();
                $table->text('contact')->nullable();

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

            });
        }
albus_severus
  • 3,626
  • 1
  • 13
  • 25