S
S
Sergey Khlopov2019-05-09 14:07:43
Laravel
Sergey Khlopov, 2019-05-09 14:07:43

How to implement cascading deletion not only from junction table in Laravel?

Hello, tell me please, I have a model Region and Image, a many-to-many relationship is defined between them. Accordingly, in the database of the table: regions, images, and the linking table image_region.
Link table migration here:

Schema::create('image_region', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('region_id');
            $table->foreign('region_id')->references('id')->on('regions')->onDelete('cascade');
            $table->unsignedBigInteger('image_id');
            $table->foreign('image_id')->references('id')->on('images')->onDelete('cascade');
        });

Actually, the problem I have is that when I delete a Region, the pictures attached to it in the Image table remain, but of course, the id mappings are deleted from the image_region. I would like that when deleting a Region, the pictures from the Image table attached to it and the id mappings from the image_region table are deleted. I have an idea, of course, when deleting a Region, get the images from the Image attached to the corresponding Region and delete them. But maybe there is another solution. Please advise, thanks in advance for your reply.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question