我有一个资源表和一个resources_votes表,其中每次用户喜欢[vote_id 1]或不喜欢[vote_id 2]资源时我都有一条记录。现在我需要检索从最喜欢的那些订购的所有资源信息到更少的资源信息,并且因为当我使用->with('votes')
时资源有许多resources_votes,它返回一个对象数组,每个resources_votes与资源ID相关。
有没有办法可以计算一个资源有多少正投票[vote_id =2]
,添加一个包含此计数的字段,并从大多数投票到较少投票的订单排序?
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
},
]
},
答案 0 :(得分:2)
你可以通过这样的热切加载获得它
votes_count
这将返回名为.data
descr byte "This program will check if the PIN is valid with the stated guidelines",0dh,0ah
intro byte "-------------------------------------",0dh,0ah
byte "Please enter a five digit PIN that validates the guidelines:",0dh,0ah
byte "-------------------------------------",0dh,0ah
PIN dword ?
msg1 byte "PIN is valid",0dh,0ah
msg2 byte "PIN is not valid",0dh,0ah
.code
main PROC
mov edx,OFFSET intro ;intro into edx
call WriteString ;display intro
call Crlf ;new line
call ReadInt
mov PIN,eax ;get PIN from user
call validate
invoke ExitProcess,0
main ENDP
validate PROC
mov si,OFFSET PIN
.IF ([esi]>=5) && ([esi]<=9) ;if 1st digit is within 5-9
add esi, TYPE PIN
.ELSE
mov edx,OFFSET msg2
call WriteString
.IF([esi]>=2) && ([esi]<=5) ;if 2nd is within 2-5
add esi, TYPE PIN
.ELSE
mov edx,OFFSET msg2
call WriteString
.IF([esi]>=4) && ([esi]<=8) ;if 3rd is within 4-8
add esi, TYPE PIN
.ELSE
mov edx,OFFSET msg2
call WriteString
.IF([esi]>=1) && ([esi]<=4) ;if 4th is within 1-4
add esi, TYPE PIN
.ELSE
mov edx,OFFSET msg2
call WriteString
.IF([esi]>=3) && ([esi]<=6) ;if 5th is within 3-6
mov edx,OFFSET msg1
call WriteString
.ELSE
mov edx,OFFSET msg2
call WriteString
.ENDIF
validate ENDP
end main
的列。您需要调用该列以显示计数。
答案 1 :(得分:0)
您可以使用Eloquent Accessors。
在模型中创建以下自定义字段和功能
protected $appends = ['positiveVotesCounter'];
然后,创建以下函数以检索自定义数据。
function getPositiveVotesCounterAttribute() {
$totalPositiveVotes = 0;
foreach($this->votes as $vote) {
if($vote['id'] == 2) {
$totalPositiveVotes++;
}
}
return $totalPositiveVotes;
}
更多信息可以在这里找到: https://laravel.com/docs/5.4/eloquent-mutators
祝你好运!