D
D
Dimon3x2019-04-24 23:23:37
Laravel
Dimon3x, 2019-04-24 23:23:37

How to remove related notifications for a post when it is deleted?

Notification box component.
When you comment on an article, notifications come, and they are marked as read,
how can I make sure that when the article is deleted, these notifications are deleted from the table?
After all, a separate id of the article is not written to the table, but everything is written to the data column.
Maybe expand the notifications table and add a separate post id column?
Or is there already a solution?
After all, take all the records and sort through each one, then look for whether there is a post id in this object,
this will be too much load and this is not logical.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dimon3x, 2019-04-25
@Dimon3x

This is it

$notifications = DB::table('notifications')
                ->whereJsonContains('data->post_id', $id)
                ->get();
        
        dd($notifications);

P
Prego666, 2019-04-26
@Prego666

In general, from the point of view of design, of course, it is better to make a foreign key for the post and then simply delete it cascaded, but if you do it in a bastard, then you can, as already suggested.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question