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

daniprog91's avatar

Laravel 8 reading post with public friends private constraints

hello, I'm having trouble implementing post reading by setting up public friends private links. I have a bachecas table in which the posts are contained, with these columns id_post, content, privacy

privacy = 1 "public" privacy = 2 "friends" privacy = 3 "private"

then i have a user_follower table with following_id, follower_id, relationship tables

relationship = 1 "fan" relationship = 2 "friend"

and then the user table containing the data of the registered users.

What I would like to do is if there are following_id == 1 and follower_id == 2 and the relationship == 2 "friend" id_post.privacy == 2 "friends" or id_post.privacy == 1 "public" id2 can view messages from id1, while if id_post.privasy == 3 "private" the message can only be viewed by the current user.

With the following code I can visualize it correctly, but I would like to add the privacy constraint

    $posts = bacheca::join('users','users.id', '=', 'bachecas.id_utente')
    ->whereHas('relazioneFollower', function ($query) use ($current_id) {//verifico se l`utente e` bloccato nella tabella black_lists tramite la funzione blocks nella pagina User.php
        $query->where('user_follower.follower_id', '=', $current_id)
        ->where('user_follower.following_id', '=', 'users.id')
        ->where('bachecas.privacy','=','user_follower.relazione');
    })
    
    ->whereIn('id_utente', function($query) use($current_id)
    {
    $query->select('following_id')
    ->from('user_follower')
    ->where('follower_id',$current_id);
    })->orWhere('id_utente', $current_id)

    ->orderBy('bachecas.created_at', 'desc')
    ->select('users.id', 'users.name', 'users.cognome', 'users.fotoProfilo','bachecas.title','bachecas.contenuto_post','bachecas.file','bachecas.immagine','bachecas.video','bachecas.audio','bachecas.posizione','bachecas.privacy','bachecas.created_at','bachecas.updated_at')
    ->get();

public function relazioneFollower() {

return $this->belongsToMany(bacheca::class, 'user_follower','following_id','follower_id');

}

thank you

0 likes
0 replies

Please or to participate in this conversation.