2

How do I fetch data for specific authenticated user in a Laravel many-to-many relationship? I have a page where it will display all the latest threads from all communities. However, I want to make sure it will only show the threads from a community where the currently login user belongs to. (I'm not sure if I made sense)

User.php

public function communities()
{
    return $this->belongsToMany(Community::class)->withTimestamps();
}

public function threads()
{
    return $this->hasMany(Thread::class);
}

Community.php

public function users()
{
    return $this->belongsToMany(User::class)->withTimestamps();
}

public function threads()
{
    return $this->hasMany(Thread::class);
}

Thread.php

public function user()
{
    return $this->belongsTo(User::class, 'author_id', 'id');
}

public function community()
{
    return $this->belongsTo(Community::class);
}

This is how I fetched the data with no logged in user.

$threads = Thread::with(['user', 'community'])->orderBy('created_at')->paginate(20);

What I want to achieve is to get the latest threads from communities where the logged in user belongs to.

Wahyu Kristianto
  • 8,719
  • 6
  • 43
  • 68
user3569641
  • 892
  • 1
  • 17
  • 50

2 Answers2

0

To get the currently logged in user, we can use the auth()->user() helper function and get the communities like the following:

$communities = auth()->user()->communities;

But you need the threads of those communities, so we must add in more logic where we fetch the IDs of those communities you follow, then retrieve all the threads that those communities contain to then perform whatever queries you need at the end.

$communityIds = auth()->user()->communities->pluck('id');
$threads = Thread::whereIn('community_id', $communityIds)->latest()->paginate(20);

I haven't tested this code since I don't have the same structure, so you may need to debug a thing or two to fix the queries but it should theoretically perfectly match your structure.

gowl
  • 314
  • 1
  • 11
  • 1
    This only shows current logged in user's own threads. I need to fetch the latest threads in all communities the user belongs to regardless if the user posted it or not. – user3569641 Feb 15 '22 at 12:56
  • 1
    @user3569641 I see now. Although I am still curious if you have a structure to save what users follow which communities. Have you set up the aforementioned many-to-many relationship using a junction table? If not, I recommend that you look [Laravel Acquaintances](https://github.com/multicaret/laravel-acquaintances) which will help set everything up for you and is extremely easy to use. If you're not interested, I can modify my answer after you clear up to me how your community Follow system works. – gowl Feb 15 '22 at 13:24
  • 1
    I'm going to take a look on that package. Thank you. With regards to the structure, I used many-to-many relationship. I have a pivot table for `community_user`. Since users can have multiple communities and communities vice versa. That's why I need to get all the threads in all communities the user belongs to. – user3569641 Feb 15 '22 at 13:37
  • I have edited my answer to better suit your problem. Let me know if it works or not. I'm not fluent with Laravel, so I'm just trying to help out in any way I can ':D – gowl Feb 16 '22 at 09:46
0

You can use auth to access to authenticated user, there are two ways to using it, First use auth directly and second use Auth Facade:

  1. Directly

    $threads = auth()->users()->threads()

  2. Facade

    $threads = Auth::user()->threads()

ahmad
  • 11
  • 5
  • 1
    Hi there! This is the same with @gowl's original answer. This only provide the threads which the user posted it self. – user3569641 Feb 15 '22 at 13:40
  • It get 20 of users community threats $thread = Auth::users()->with('community.threads')->paginate(20); to get latest you can use orderByDesc() method – ahmad Feb 15 '22 at 14:30
  • Or if with dosent work there: $thread = Auth::users()->community()->threads()->paginate(20); – ahmad Feb 15 '22 at 14:35