How is a pivot table created by Laravel?

后端 未结 5 1465

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:27

    I use Jeffrey Way's Laravel-4-Generators or Laravel-5-Generators-Extended.

    then you can just use this artisan command:

    php artisan generate:pivot table_one table_two
    
    0 讨论(0)
  • 2020-12-13 02:31

    For Many to Many relationships you can create the Migration file of the Database manually like this:

    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    
    class CreateAccountTagTable extends Migration
    {
    
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('account_tag', function (Blueprint $table) {
                // $table->timestamps(); // not required
                // $table->softDeletes(); // not required
    
                $table->integer('account_id')->unsigned();
                $table->foreign('account_id')->references('id')->on('accounts');
    
                $table->integer('tag_id')->unsigned()->nullable();
                $table->foreign('tag_id')->references('id')->on('tags');
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::drop('account_tag');
        }
    }
    

    Note: in case you have timestamps on the pivot table you must set withTimestamps on the relationship of both ends like this:

    return $this->belongsToMany(\Mega\Modules\Account\Models\Tag::class)->withTimestamps();
    

    .

    return $this->belongsToMany(\Mega\Modules\Account\Models\Account::class)->withTimestamps();
    
    0 讨论(0)
  • 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');
        });
    }
    
    0 讨论(0)
  • 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');
         });
    }
    
    0 讨论(0)
  • 2020-12-13 02:46

    It appears as though the pivot table does need to be created manually (i.e. Laravel does not do this automatically). Here's how to do it:

    1.) Create a new migration, using singular table names in alphabetical order (default):

    php artisan make:migration create_alpha_beta_table --create --table=alpha_beta
    

    2.) Inside the newly created migration, change the up function to:

    public function up()
    {
        Schema::create('alpha_beta', function(Blueprint $table)
        {
            $table->increments('id');
            $table->integer('alpha_id');
            $table->integer('beta_id');
        });
    }
    

    3.) Add the foreign key constraints, if desired. (I haven't gotten to that bit, yet).


    Now to seed, say, the alpha table, using keys from beta, you can do the following in your AlphaTableSeeder:

    public function run()
    {
        DB::table('alpha')->delete();
    
        Alpha::create( array( 
            'all'           =>  'all',
            'your'          =>  'your',
            'stuff'         =>  'stuff',
        ) )->beta()->attach( $idOfYourBeta );
    }
    
    0 讨论(0)
提交回复
热议问题