How is a pivot table created by Laravel?

后端 未结 5 1468

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条回答
  •  南方客
    南方客 (楼主)
    2020-12-13 02:37

    To expand on Ben's answer (I tried to edit it but reviewers said it added too much):

    To add the foreign key constraints, make sure if alpha id is unsigned, alpha_id is also unsigned in the pivot table. This migration would run after (2) in Ben's answer since it alters the table created then.

    public function up()
    {
        Schema::table('alpha_beta', function(Blueprint $table)
        {
            $table->foreign('alpha_id')->references('id')->on('alpha');
            $table->foreign('beta_id')->references('id')->on('beta');
        });
    }
    

提交回复
热议问题