<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTeachersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
if (!Schema::hasTable('teachers'))
{
Schema::create('teachers', function (Blueprint $table) {
$table->increments('id');
$table->string('teacher_name');
$table->unsignedInteger('user_id');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
});
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('teachers');
}
}
Now You want to set the foriegn key
of as id of users
table in teachers
table
AM I Clear
Now
adding the column to techers table and adding foriegn key constarint
METHOD ONE
To Add the Column
$table->unsignedInteger('user_id');
To Add Primary Key
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
Explaination now user_id
is field name in teachers table references('id')
is referencing the id on('users')
is on user table
METHOD TWO
To Add the Column
$table->integer('user_id');
To Add Primary Key
$table->foreign('user_id')->references('id')->on('users')->unsigned()->onDelete('cascade');
Explaination now user_id
is field name in teachers table references('id')
is referencing the id on('users')
is on user table ,unsigned()
adding unsignedinteger
to column
If Your issue is not solved kindly comment below
Hope it helps