You need to enable JavaScript to run this app. Laravel8实现公开/好友/私密三类权限的帖子查看功能开发咨询
优惠活动
大模型
产品
解决方案
定价
更多

Laravel8实现公开/好友/私密三类权限的帖子查看功能开发咨询

解决方案

一、直接修正现有查询

你原有代码的核心问题是关联条件逻辑完全错位:将bachecas.privacy和user_follower.following_id绑定不符合业务规则,且未对or条件做分组会导致全局逻辑混乱,同时你错误使用了不存在的user_follower.stato字段,实际对应字段应为relationship。
修改后的代码如下:

public function get (Request $request)
{
    // 先获取当前登录用户ID,此处假设你已完成身份验证赋值$current_id
    $current_id = auth()->id();
    $posts = bacheca::join('users', 'users.id', '=', 'bachecas.id_utente')
        // 用leftJoin关联用户关系表,避免过滤掉自己发布的帖子、无好友关系的公开帖子
        ->leftJoin('user_follower', function ($join) use ($current_id) {
            $join->on('user_follower.following_id', '=', 'bachecas.id_utente')
                ->where('user_follower.follower_id', '=', $current_id)
                ->where('user_follower.relationship', '=', 2);
        })
        // 权限判断逻辑:满足任意一个条件即可查看
        ->where(function ($query) use ($current_id) {
            // 条件1:公开帖子所有人可见
            $query->where('bachecas.privacy', 1)
                // 条件2:好友可见帖子,且当前用户是发布者的好友
                ->orWhere(function ($q) {
                    $q->where('bachecas.privacy', 2)
                        ->whereNotNull('user_follower.id');
                })
                // 条件3:私密帖子仅发布者本人可见
                ->orWhere(function ($q) use ($current_id) {
                    $q->where('bachecas.privacy', 3)
                        ->where('bachecas.id_utente', $current_id);
                });
        })
        ->orderBy('bachecas.created_at', 'desc')
        ->select('users.name', 'users.cognome', 'users.fotoProfilo','bachecas.contenuto_post', 'bachecas.id_post')
        ->get();
    return response()->json($posts);
}

二、更优的多对多关联实现方案

通过Laravel关联封装权限逻辑,代码可读性和可维护性更高,后续修改权限规则不需要改动业务层代码。

步骤1:定义模型关联

首先在User模型中添加好友关联和帖子关联:

// app/Models/User.php
public function posts()
{
    return $this->hasMany(Bacheca::class, 'id_utente');
}

public function friends()
{
    return $this->belongsToMany(User::class, 'user_follower', 'follower_id', 'following_id')
        ->wherePivot('relationship', 2);
}

在Bacheca模型中添加发布者关联和可见性查询作用域:

// app/Models/Bacheca.php
public function user()
{
    return $this->belongsTo(User::class, 'id_utente');
}

public function scopeVisibleTo($query, $userId)
{
    return $query->where(function ($q) use ($userId) {
        $q->where('privacy', 1)
            ->orWhere(function ($q2) use ($userId) {
                $q2->where('privacy', 2)
                    ->whereIn('id_utente', function ($sub) use ($userId) {
                        $sub->select('following_id')
                            ->from('user_follower')
                            ->where('follower_id', $userId)
                            ->where('relationship', 2);
                    });
            })
            ->orWhere(function ($q3) use ($userId) {
                $q3->where('privacy', 3)
                    ->where('id_utente', $userId);
            });
    });
}

步骤2:业务代码调用

调用逻辑非常简洁,后续所有需要拉取可见帖子的场景都可以复用这个作用域:

public function get (Request $request)
{
    $current_id = auth()->id();
    $posts = Bacheca::visibleTo($current_id)
        ->with('user:id,name,cognome,fotoProfilo')
        ->orderByDesc('created_at')
        ->get()
        ->map(function ($post) {
            return [
                'name' => $post->user->name,
                'cognome' => $post->user->cognome,
                'fotoProfilo' => $post->user->fotoProfilo,
                'contenuto_post' => $post->contenuto_post,
                'id_post' => $post->id_post
            ];
        });
    return response()->json($posts);
}

内容的提问来源于stack exchange,提问作者Daniele Baglivo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.21 08:06:10