0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Laravel】Laravelの外部キーの設定方法

Posted at
        Schema::create('line_items', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('cart_id');
            $table->unsignedBigInteger('product_id');
            $table->integer('quantity');
            $table->timestamps();

            $table->foreign('cart_id')
              ->references('id')
              ->on('carts');
            $table->foreign('product_id')
              ->references('id')
              ->on('products');
        });

これはmigrationファイルの一部

            $table->foreign('cart_id')  // ここが外部キー
              ->references('id')  //外部キーに対応する親テーブルの主キー
              ->on('carts'); //親テーブル名

外部キーの設定の記述は上記。
Laravelは全体的にRailsに比べて記述が多い気がする。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?