i used laravel 8 and PHP version 7.4
but why always error. if I use PHP version 8 it doesn't error
i don't know why
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
so i want create 3 roles on the table, but always error foreign key constraint
table users
<?php
use App\Models\Role;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id(); // primary key
// main data
$table->bigInteger('nidn')->unique(); // bersifat unik
$table->string('fullname');
$table->string('email')->unique();
$table->string('password');
// detail
$table->enum('faculty', ['FTIK', 'FEIS']);
$table->string('major')->nullable();
$table->string('telp')->unique()->nullable();
$table->date('birthdate')->nullable();
$table->enum('gender', ['Pria', 'Wanita']);
$table->text('address')->nullable();
$table->text('image')->nullable();
// identifier
$table->unsignedBigInteger('role_id');
$table->foreign('role_id')->references('id')->on('roles');
// $table->boolean('is_active')->default(true);
// $table->dateTime('last_login')->nullable();
// system
$table->timestamp('email_verified_at')->nullable();
$table->rememberToken();
$table->timestamps();;
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
table roles
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateRolesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('roles');
}
}
user model
public function role(){
return $this->belongsTo(Faculty::class, 'role_id', 'id');
}
role model
public function user(){
return $this->hasMany(User::class);
}
@mhmmdva please change the filename for the roles table migration. The users table migration is (probably)
2014_10_12_000000_create_users_table; whereas the roles table is some date after 2014_10_12 - do you understand now?
Please or to participate in this conversation.