talhaatsix's avatar

Get Group Members | many to many | api | prepare response

I have Users and Groups tables .

Group have many members .

So i create middle table

group_member 
    user_id 
        group_id 
        enum( status : pending/member/admin) 

users and groups have many to many relation

Problem :

i want get only members of a group with pagination

0 likes
2 replies
talhaatsix's avatar

My middle table is group_member

user_id     bigint(20) unsigned             NO      PRI     
group_id    int(10) unsigned                    NO      PRI     
status      enum('pending','member','admin')    NO      pending 
created_at  timestamp   YES         
updated_at  timestamp   YES         

I created model to this table as well

class GroupMember extends Model
{
    public $incrementing = false;
    protected $perPage = 10;

    protected $table = 'group_member';
    protected $casts = [
        'user_id' => 'int',
        'group_id' => 'int'
    ];

    public function group()
    {
        return $this->belongsTo(\App\Group::class);
    }

    public function member()
    {
        return $this->belongsTo(\App\User::class,'user_id')->select('id','avatar','slug','name');
    }
}

Group Model is this


class Group extends Model
{   
    protected $casts = [
        'user_id' => 'int',
    ];
    protected $fillable = [
        'name',
        'slug',
        'about',
        'website',
        'invitation_message',
        'cover',
        'privacy',
        'user_id'
    ];
    
    public function getCoverAttribute($value) {
        $path = env('IMAGES_URL');
        return ($value) ? $path . '/' . $value : '/static/images/user_cover.jpg';
    }

    
    
        public function members()
    {
        return $this->belongsToMany(\App\User::class, 'group_member')->withPivot('status');
    }
}

is used your provided solution .

i get members with pagination that ok

problem is its getting too much info of user

{
    "current_page": 1,
    "data": [
        {
            "id": 1,
            "name": "rrrrrl",
            "email": "rrrrrrrrr",
            "slug": "rrrr-1",
            "type": "1",
            "status": "Active",
            "phone": "159951",
            "country_id": 1,
            "city_id": 6486,
            "is_paid": false,
            "language_id": 1,
            "currency_id": 46,
            "about": "My Bio",
            "profession": "ABC",
            "education": "BSCS",
            "address": "123 street",
            "avatar": "http:\/\/local.qwe.com\/avatars\/1\/aEP5CDsbn4AtHw0Qb1zZoa1uS0aqRUK278RXCZQh.jpeg",
            "cover": "\/static\/images\/user_cover.jpg",
            "email_privacy": "Friends",
            "phone_privacy": "Friends",
            "education_privacy": "Public",
            "location_privacy": "Public",
            "pitch_privacy": "Public",
            "register_date": "2019-07-10 07:25:38",
            "created_at": "2019-07-10 07:21:43",
            "updated_at": "2019-08-31 13:28:30",
            "pivot": {
                "group_id": 1,
                "user_id": 1,
                "status": "pending"
            }
        }
    ],
    "first_page_url": "http:\/\/localhost\/pitchie-laravel-api\/public\/group-members?page=1",
    "from": 1,
    "last_page": 2,
    "last_page_url": "http:\/\/localhost\/pitchie-laravel-api\/public\/group-members?page=2",
    "next_page_url": "http:\/\/localhost\/pitchie-laravel-api\/public\/group-members?page=2",
    "path": "http:\/\/localhost\/pitchie-laravel-api\/public\/group-members",
    "per_page": 1,
    "prev_page_url": null,
    "to": 1,
    "total": 2
}

for member i just want slug , name , avatar thats it...

Please or to participate in this conversation.