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

Tectron's avatar

User groups vs roles

Hi, I am looking to make an app based in user groups, the main functionality is any user can create a user group, then send invitations to other users for membership. I was reading about differences at http://stackoverflow.com/questions/7770728/group-vs-role-any-real-difference

Looking for something already written for Laravel I can see a lot for user roles management like spatie, sentinel, sentry, laravel-acl, entrust, roles, shinobi, and little about user groups management, only https://atorscho.gitbooks.io/membership/content/index.html seems to be user group oriented, sentry was user group oriented too but for Laravel 5 has been replaced by Sentinel which is for roles.

Which one should I choose to implement the main functionality?

0 likes
6 replies
willvincent's avatar

Probably what you'll what here is a polymorphic relationship -- specifically a polymorphic many to many -- that associates models of various kinds with a particular 'group'

So, basically create a group model, that just holds the different groups, these could have names, etc.. then any other model that can belong to a group, be it a user, post, etc.. can be related.

No need for a special package needed for that, just relationships.

willvincent's avatar
users
    id - integer
    name - string
    ..etc
posts
    id - integer
    title - string
    body - text
        ..etc

groups
    id - integer
    name - string

groupables
    group_id - integer
    groupable_id - integer
    groupable_type - string
class User extends Model {

    public function groups()
    {
        return $this->morphToMany('App\Group', 'groupable');
    }

}

class Post extends Model {

    public function groups()
    {
        return $this->morphToMany('App\Group', 'groupable');
    }

}
class Group extends Model {

    public function posts()
    {
        return $this->morphedByMany('App\Post', 'groupable');
    }

    public function users()
    {
        return $this->morphedByMany('App\User', 'taggable');
    }

}

Then you should be able to do things like:

$group_users = Group::find($group_id)->users;
$user = User::find($user_id)->groups;
Tectron's avatar

Make sense, thank you very much I will implement it on my project and post results.

symfonycoda's avatar

@Tectron do you have an update for how you are getting on with this project?

I am going to try building a small social network as a test project and I am looking to possibly do something similar to yourself with User Groups

So anything that you have learnt (good or bad) could be very useful to me.

Thanks

Tectron's avatar

Hello @symfonycoda, finally I used regular many to many relations as I think polimorphics are not needed in my case. I am sure with polimorphic relations I could achieve same results but for now and as you it is a test proyect for me I would like to keep things simple, so for groups I just created a "belongsToMany(User::class)" and same for users "$this->belongsToMany(Group::class)".

Please or to participate in this conversation.