简体   繁体   English

多对多关系查询。 拉拉韦尔

[英]Many to Many relationship query. Laravel

I'm trying to figure out how I can do a query within a query. 我试图弄清楚如何在查询中执行查询。 (if that even makes sense) I want to grab all the activities. (如果有道理的话)我想参加所有活动。 For each activity I want to get the count of users that did the activity. 对于每个活动,我想要获取执行该活动的用户数。 Then I want to order all the activities in DESC order based on the amount of users that did each activity. 然后,我想根据执行每个活动的用户数量,以DESC顺序对所有活动进行排序。 I'm basically making a "Popular Activities Page" where I show the all activities starting with the activity done by the most users. 我基本上是在制作“热门活动页面”,其中显示了所有用户从大多数用户开始的活动开始的所有活动。

I have 3 main tables for this 我为此有3个主表

users 使用者

| id | name | password | email | created_at |

activities 活动

| id | title | description | created_at |

resource This is a table I'm using for posts which shows which user did which activity. 资源这是我用于帖子的表,该表显示了哪个用户执行了哪个活动。 (Users can show what activities they did, and attach media and locations to the post) (用户可以显示他们做了什么活动,并将媒体和位置附加到帖子中)

| id | user_id | activity_id | media_id | location_id | created_at |

Here are my models for each table 这是我每个桌子的型号

User Model 用户模型

class User extends Eloquent {

    protected $table = 'users';

    /**
     * get the activities associated with the given user
     * @return mixed
     */
    public function activities()
    {
        return $this->belongsToMany('Acme\Activities\Activity', 'resource', 'activity_id');
    }

    public function posts(){
        return $this->hasMany('Acme\Resource\Resource');
    }

    public function media()
    {
        return $this->hasMany('Acme\Media\Media');
    }

    public function locations()
    {
        return $this->hasMany('Acme\Locations\Location');
    }
}

Activity Model 活动模型

class Activity extends Eloquent  {

    protected $table = 'activities';


    public function posts(){
        return $this->hasMany('Acme\Resource\Resource', 'resource_id');
    }

    /**
     * get the users associated with the given activity card
     * @return mixed
     */
    public function users()
    {
        return $this->belongsToMany('Acme\Users\User', 'resource', 'user_id');
    }

}

Resource Model 资源模型

class Resource extends Eloquent {

    protected $table = 'resource';


    public function user()
    {
        return $this->belongsTo('Acme\Users\User', 'user_id');
    }

    public function activities()
    {
        return $this->belongsTo('Acme\Activities\Activity', 'activity_id');
    }

    public function media()
    {
        return $this->hasMany('Acme\Media\Media', 'media_id');
    }

    public function locations()
    {
        return $this->belongsTo('Acme\Locations\Location', 'location_id');
    }
}

I know I can get all activities using 我知道我可以使用

Activity::get()

I can get a user count for a specific activity using 我可以使用来获取特定活动的用户数

User::whereHas('resource', function($q) use ($activity_id){
    $q->where('activity_id', $activity_id);
})->get()->count();

but I don't know how I can put all of this together in order to get all Activities sorted by user count, starting with the activity with the highest user count. 但我不知道如何将所有这些活动放在一起,以便按用户数从所有用户数最高的活动开始对所有活动进行排序。

How would I make this query using eloquent? 我该如何使用雄辩的方式进行此查询?

Thanks in advance! 提前致谢!

try 尝试

Resource::select(DB::raw('*, COUNT(distinct(user_id)) as user_count'))->group_by('activity_id')->order_by('user_count', 'desc')->get();

You could then do this 然后,您可以执行此操作

$results = Resource::select(DB::raw('*, COUNT(distinct(user_id)) as user_count'))->group_by('activity_id')->order_by('user_count', 'desc')->get();

foreach ($results as $result) {
    $activity = Activity::where('id','=',$result->activity_id)->first();
    // do stuff to display data for this activity like
    // $activity->title or $activity->description
    $count = $result->user_count; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM