How is a pivot table created by Laravel?

后端 未结 5 1501

In Laravel 4, when working with many-to-many relationships as described in the 4.2 docs, how can I actually get Laravel to create the pivot table for me?

Do I need

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-13 02:43

    1. Create new migration:
    php artisan make:migration create_alpha_beta_table --create=alpha_beta
    
    1. Inside the newly created migration:
    public function up() {
        Schema::create('alpha_beta', function(Blueprint $table) {
                $table->increments('id');
                $table->unsignedBigInteger('alpha_id');
                $table->unsignedBigInteger('beta_id');
                // foreign keys
                $table->foreign('alpha_id')->references('id')->on('alphas');
                $table->foreign('beta_id')->references('id')->on('betas');
         });
    }
    

提交回复
热议问题