dimanche 7 avril 2019

How can I get the commnunity followers by using laravel Eloquent relationship

I am trying to create the community system by using Laravel eloquent relationship, I am unable to solve the problem, please help

Basically I am trying to create the event system which will based on the communities (e.g: Business & Professional, Health & Wellness, Science & Technology, etc).

To achieve this I created the following tables

1) Users

2) event_categories (you can say communities)

3) community_user (user_id, community_id)

Controller

public function communityBySlug($slug){
        $eventCategory = EventCategories::where(['slug' => $slug])->first();
        $eventCategoryId = $eventCategory->id;


        // Getting users by community id
        $users = CommunityUsers::find(8)->users();

        return Response::json(
            [
                'data' => $eventCategory,
                'community_followers' => $users
            ]
        );
    }

Model: CommunityUsers

class CommunityUsers extends Model
{
    protected $fillable = ['community_id', 'user_id'];
    protected $guarded = [];

    public function Users(){
        return $this->belongsToMany(User::class, 'users');
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire