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

APPLE199's avatar

Help me with this query - showing all threads that the user is able to see

A user can only view and post on a thread that the user is a "recipient" of. I have explained it in more detailed down below.

My tables structure:

threads table: user_id, subject, recipients (contains serialized array of users)

messages table: thread_id, user_id, body

participants table: thread_id, user_id, read_at, deleted_at

So what I want is a way to show all threads that the user is a recipient of. And if they have deleted the thread (each time a thread is created, the participants table is populated by each recipient). For instance, if a thread has 3 recipients, 3 rows will be created in the participants table with their user ID. And there they have an option to delete the thread to not see it when they are viewing all threads that "belongs" to them.

I do not know how I can figure out a way to show threads to users who are the recipients of the threads. Please help me tackle this issue. Thanks!

I tried the following code but no luck.

    $threads = Thread::all();
    $participant = Participant::where(['user_id' => uid()])->get();
    foreach ($threads as $thread) {
       $recipients = unserialize($thread->recipients);
        if (in_array(uid(), $recipients) || $thread->user_id == uid()) {
           $messages = (object)Participant::where(['user_id' => uid(), 'thread_id' => $thread->id])->paginate(10);
        }
    }

Demo:

A thread has been created:

The recipients' user_id's are: 4, 7, 8.

threads table:

id  | user_id | subject | recipients
-------------------------------------------------------------------
1   |       1 | Hello   | a:1:{s:7:"user_id";a:3:{i:0;i:4;i:1;i:7;i:2;i:8;}}

messages table:

id  | user_id | thread_id | body
--------------------------------------------
1   |       1 |         1 | How you doing?

participants table:

id  | user_id | thread_id | read_at
--------------------------------------------
1   |       1 |         1 | 2016-04-13 00:03:23
--------------------------------------------
1   |       4 |         1 | NULL
--------------------------------------------
1   |       7 |         1 | NULL
--------------------------------------------
1   |       8 |         1 | NULL
0 likes
4 replies
APPLE199's avatar

@Prez how do you recommend I set up the database tables for that?

Thank you.

APPLE199's avatar

Thanks @prez I created that pivot table. Now I have no Idea what to do. I did create a belongs to relationships in the Thread model as such:

 public function users()
    {
        return $this->belongsToMany('App\User');
    }

Please help. Thank you!

APPLE199's avatar

Anyone? Please help me out. I have got a pivot table but it's not really working as expected.

APPLE199's avatar

@Prez I've watched it a few times. I understand what it is and can use many to many relationship with other things such as user roles but not really for this.

Please or to participate in this conversation.