NON STOP TECH BLOG

ノンストップで書きまくる技術ブログ

Laravelで外部キーを設定する

忘れそうなのでメモ

user table

カラム
id increments

performance table

カラム
user_id integer

みたいなリレーションをもたせたいときのmigrationの書き方

performance tableへ変更を加えるmigrationファイル

public function up()
{
    Schema::table('recipes', function (Blueprint $table) {
        $table->integer('user_id')->unsigned()->change();
        $table->foreign('user_id')->references('id')->on('users');
    });
}