Search code examples
phplaravelforeign-keys

How to set the foreign key name in php laravel?


How can I set the name of a foreign key in php laravel?

Schema::table('TABLE_NAME', function (Blueprint $table) {
        $table->foreign(XXX)
            ->references(XXX)
            ->on('REF_TABLE')
            ->onDelete('cascade');

            // HOW TO ACHIEVE SOMETHING LIKE THIS?
            //->name('Custom name of foreign key.')
            //->comment('Custom comment for foreign key.')        
    });

Solution

  • You can specify a custom name by filling the second foreign param :

    ->foreign('XXX', 'my_custom_name')