define one foreign key have multiple refrence

Posted 5 months ago by [email protected]

Hi. i have three table Posts,Videos and Comments. one of column inside comment table have foreign key role. i want this foreign key have relation with two columns inside post and video.but migration only create one foreign key relation and can't create second relation.

Posts Table:


 Schema::create('posts', function (Blueprint $table) {
            $table->increments('p_id');
            $table->text("title");
            $table->text("body");
            $table->timestamps();
        });

Videos Table:


        Schema::create('videos', function (Blueprint $table) {
            $table->increments('v_id');
            $table->string("url");
            $table->text("title");
            $table->timestamps();
        });

Comments Table:


        Schema::create('comments', function (Blueprint $table) {
            
            $table->increments('c_id');
            $table->text("body");
            $table->integer("commentable_id")->unsigned();
            $table->string("commentable_type");
          $table->foreign('commentable_id')->references('v_id')->on('videos');
          
            $table->foreign('commentable_id')->references('p_id')->on('posts'); //Give Error
            $table->timestamps();
       
        });

when i want define second relation(foreign key) that give error.Please help me

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.