SkyCoder
4 months ago

Multi relation foreign keys

Posted 4 months ago by SkyCoder

If I have 3 tables:

User, Conversation, Messages

Between User and conversation I have:

$table->foreign('user_id')
                  ->references('id')
                  ->on('users')
                  ->onDelete('cascade');

And between conversation and messages:

$table->foreign('conversation_id')
                  ->references('id')
                  ->on('conversations')
                  ->onDelete('restrict');

If someone accidentally try to delete conversation, it should stop him because of messages restriction. But same way user is deleted I want to delete both conversation and messages.

How can I accomplish that?

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