简体   繁体   English

Laravel:按关系上的count id字段查询订单

[英]Laravel: Order query by count id field on relation

I have a resources table, and a resources_votes table in which I have a record for everytime a user likes [vote_id 1] or dislikes [vote_id 2] a resource. 我有一个资源表和一个resources_votes表,其中每当用户喜欢[vote_id 1]或不喜欢[vote_id 2]资源时,我都有一条记录。 now I need to retrieve all the resource information ordered from most liked to the less one, and since resource has many resources_votes when I use ->with('votes') it returns an array of objects with each one of the resources_votes related to the resource ID. 现在,我需要检索从最喜欢到不喜欢的所有资源信息,并且由于当我使用->with('votes')时,资源具有许多resources_votes,它返回一个对象数组,其中每个资源与资源ID。

Is there a way I can count how many positive votes a resource has [vote_id =2] , add a field with this count and order the query from most voted to less voted? 有没有一种方法可以计算资源有多少个赞成票[vote_id =2] ,添加一个具有此计数的字段,然后将查询从投票最多到投票少排序?

PD: this is an example of a resource object with the resources_votes relationship, there you can see the votes array and the vote ID which I need to count and order according to: PD:这是一个具有resources_votes关系的资源对象的示例,在这里您可以看到投票数组和投票ID,我需要根据这些投票进行计数和排序:

    {
            "id": 2,
            "name": "aspernatur",
            "image": "http://lorempixel.com/480/480/?31738",
            "author": "Max Thiel",
            "created_by": 6,
            "reviewed_by": "Mr. Emiliano Frami",
            "lang_id": 2,
            "resource_type_id": 1,
            "status": "Borrado",
            "resource_type": "Imagen",
            "platforms": [],
            "classifications": [],
            "votes": [
              {
                "id": 2,
                "user_id": 2,
                "resource_id": 2,
                "vote_id": 1
              },
              {
                "id": 29,
                "user_id": 1,
                "resource_id": 2,
                "vote_id": 2
              },
              {
                "id": 24,
                "user_id": 12,
                "resource_id": 2,
                "vote_id": 1
              },
            ]
          },

You can get it with eager loading like this 您可以通过这样的热切加载来获得它

$resources = Resource::withCount(['votes' => function ($query) {
    $query->where('vote_id', '=', '2'); //count positive votes only
}])->get();

This will return a column named votes_count . 这将返回一个名为votes_count的列。 You need to call that column to display the count. 您需要调用该列以显示计数。

You can use Eloquent Accessors. 您可以使用雄辩的访问者。

Create the following custom field and function inside your model 在模型中创建以下自定义字段和功能

protected $appends = ['positiveVotesCounter'];

Then, create the following function to retrieve your custom data. 然后,创建以下函数以检索您的自定义数据。

function getPositiveVotesCounterAttribute() {
    $totalPositiveVotes = 0;
    foreach($this->votes as $vote) {
        if($vote['id'] == 2) {
            $totalPositiveVotes++;
        }
    }

    return $totalPositiveVotes;
}

More info can be found here: https://laravel.com/docs/5.4/eloquent-mutators 可以在这里找到更多信息: https : //laravel.com/docs/5.4/eloquent-mutators

Good luck! 祝好运!

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

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