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

dengdeng's avatar

Can't add foreign key in migration.

I have one migration to execute. And here is my code:

public function up()
    {
        Schema::table('students', function (Blueprint $table) {
            //
            $table->foreign('phone_id')->reference('id')->on('phone');
        });
    }

where i execute migration, it said i have an error in my sql. And i run php artisan migrate --pretend to output the sql:

alter table `students` add constraint `students_phone_id_foreign` foreign key (`phone_id`) references `phone` ()

As you can see, there is a () at the end of the sql. How does it come out?

0 likes
2 replies
jusahah87's avatar
Level 5

@dengdeng I think its references('id'), not reference('id'). And that should be "phones"-table, not "phone"-table. Unless you table has just one phone hard-coded into first row.

Also, the actual sql error message could help here. Just a suggestion for the future.

Please or to participate in this conversation.