How to create project_user pivot table in laravel

You can create a project_user pivot table in laravel using migration. project_user table is the pivot table used to store pivot column data with extra columns in laravel

Answers 1
  • Create project_user pivot table with additional column is_manager and order_by

    php artisan make:migration create_project_user_table --create=project_user
    
    //database\migrations\<2021_07_21_080148>_create_project_user_table.php
    <?php
    
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Support\Facades\Schema;
    
    class CreateProjectUserTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('project_user', function (Blueprint $table) {
                $table->foreignId('project_id')->constrained();
                $table->foreignId('user_id')->constrained();
                $table->boolean('is_manager')->default(false);
                $table->boolean('order_by')->nullable();
                $table->timestamps();
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('project_user');
        }
    }
    
    php artisan migrate
    
    0
  • Back to code snippet queries related laravel