Unknown column 'subscriptions' in 'where clause'

dj990311 注册会员
2023-01-25 22:53

I have very recently come across a question asking almost the same thing. So I'll plagiarize my own answer in hopes it can be tailored for you as well.

To get the currently logged in user, we can use the auth()->user() helper function. Then we must get the spaces the current user is subscribed to, so if you have a relationship in your User model, it would be perfect and you can use the following:

$space = auth()->user()->spaces;

Otherwise, you can use your code from your question to get the spaces the user is subscribed to, but I'd still advise creating a method for this inside the user model:

$spaces = Space::whereHas('subscribers', function($query){
        $query->where('id', '=', 'user_id')->get();

We got the spaces, but what you need is the threads of those spaces, so we must add in more logic where we fetch the IDs of those spaces you follow, then retrieve all the threads that those spaces contain to then perform whatever queries you need at the end.

$spaceIds = $spaces->pluck('id');
$threads = Thread::whereIn('space_id', $spaceIds)->latest()->paginate(10);

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.

About the Author

Question Info

Publish Time
2023-01-25 22:53
Update Time
2023-01-25 22:53