cmgmyr / laravel-messenger

Simple user messaging package for Laravel
MIT License
2.45k stars 514 forks source link

Help with implementation of blocked users? #337

Closed DarkB0SsS closed 4 years ago

DarkB0SsS commented 4 years ago

Hello and thank you for this amazing package!

I am building an application where I implemented this package. I want to have an option for block user. I made this functionality and now I want to implement it in the messenger.

My user model is:

public function blockedUsers()
{
    return $this->bengsToMany(User::class, 'blocked_user', 'user_id', 'blocked_id');
}

My database

Schema::create('blocked_user', function (Blueprint $table){
     $table->increments('id');

     $table->integer('blocked_id')->unsigned()->index();
     $table->foreign('blocked_id')->references('id')->on('users')->onDelete('cascade');
     $table->integer('user_id')->unsigned()->index();
     $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});

So my current query for user threads is as follows:

$threads = Thread::forUser(Auth::id())->whereHas('messages')->latest('updated_at')->get();

My question is, how to build the query so I can get only the threads from users that are not blocked?

Thank you in advance! I appreciate the help.

cmgmyr commented 4 years ago

@DarkB0SsS thanks for using the package!

Seems like you might want to extend the package's Thread model and make your own. This way you could add on more scopes, or methods, as you need them. You could either add a new scope to remove any thread with a blocked user, or filter all of the blocked user's threads out of the Eloquent collection. Just depends on how your other business logic is set up and how you'd like it to function.

I hope that helps 🙂