Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

HUGE_DICK_10_INCHES's avatar

Multi relation foreign keys

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?

0 likes
1 reply
shez1983's avatar

surely this should work? what you have already i mean. if someone deletes user, it will delete conversation & then messages...

in some case you could use events/observers too..

Please or to participate in this conversation.