Skip to content

Instantly share code, notes, and snippets.

@JeffreyWay
Last active April 28, 2016 06:19
Show Gist options
  • Select an option

  • Save JeffreyWay/6143691 to your computer and use it in GitHub Desktop.

Select an option

Save JeffreyWay/6143691 to your computer and use it in GitHub Desktop.
What else would you want on a typical pivot table...say for posts and tags.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class PivotPostTagTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('post_tag', function(Blueprint $table) {
$table->integer('post_id')->unsigned()->index();
$table->integer('tag_id')->unsigned()->index();
$table->foreign('post_id')->references('id')->on('post')->onDelete('cascade');
$table->foreign('tag_id')->references('id')->on('tag')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('post_tag');
}
}
@JeffreyWay

Copy link
Copy Markdown
Author

This will go in the new generate:pivot helper command. So the above will be dynamically generated when you run:

php artisan generate:pivot posts tags

@montogeek

Copy link
Copy Markdown

What about the alphabetical order of the tables name?

@JeffreyWay

Copy link
Copy Markdown
Author

Yeah, that's already been added.

@ipalaus

ipalaus commented Aug 2, 2013

Copy link
Copy Markdown
  1. An id auto increment will give better performance on high load.
  2. Indexes on all the fields. (Fixed)

@montogeek

Copy link
Copy Markdown

+1 Yeah, the recomendation is have an id on all your table, even the pivot tables, and what about the timestamps ?

@JeffreyWay

Copy link
Copy Markdown
Author

#2 is fixed. Just forgot to add that.

I wasn't sure what the rule was on adding an auto-increment id to joinables. But I can add it easily if that's a good rule to follow.

@JeffreyWay

Copy link
Copy Markdown
Author

Okay, so now, when you run:

php artisan generate:pivot tags posts

...it'll generate the following migration:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class PivotPostTagTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('post_tag', function(Blueprint $table) {
            $table->increments('id');
            $table->integer('post_id')->unsigned()->index();
            $table->integer('tag_id')->unsigned()->index();
            $table->foreign('post_id')->references('id')->on('post')->onDelete('cascade');
            $table->foreign('tag_id')->references('id')->on('tag')->onDelete('cascade');
        });
    }



    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('post_tag');
    }

}

@ipalaus

ipalaus commented Aug 2, 2013

Copy link
Copy Markdown

@montogeek timestamps it's more an user preference I think. I usually add it. I suggested @JeffreyWay that it could be an option.

@montogeek

Copy link
Copy Markdown

That's is because I suggested it, I use to add it too, will be great has option! :)

@JeffreyWay

Copy link
Copy Markdown
Author

I think I'm going to leave timestamps off for the time being. Takes two seconds to add it manually.

@duellsy

duellsy commented Aug 2, 2013

Copy link
Copy Markdown

I know you said you were going to leave it off, but just for reference, I +1 the addition of timestamps

@AloysA

AloysA commented Aug 3, 2013

Copy link
Copy Markdown

Could you make the foreign key constraints optional (default on is fine).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment