我從數據庫中檢索數據,就像在控制器中一樣:$users = DB::select("select users.id, users.name, users.email, count(is_read) as unread from users LEFT JOIN messagesnews ON users.id = messagesnews.from and is_read = 0 and messagesnews.to = " . Auth::id() . " where users.id != " . Auth::id() . " group by users.id, users.name, users.email");return view('admin.home.index', compact('users'));鑒于:@foreach($users as $user) @if($user->unread) <span class="pending">{{ $user->unread }}</span> @endif@endforeach干得好 我怎樣才能得到一個數字來計算它們6
2 回答

Cats萌萌
TA貢獻1805條經驗 獲得超9個贊
試試這個,
@php $totlUnread = 0; @endphp
@foreach($users as $user)
@if($user->unread)
@php $totlUnread+=$user->unread; @endphp
@endif
@endforeach
@php echo "<span class='pending'>$totlUnread</span>"; @endphp

德瑪西亞99
TA貢獻1770條經驗 獲得超3個贊
var sum = 0;
$(".pending").each(function() {
sum += parseInt($(this).html());
});
return sum;
或者
<?php
$all_unread = array_column($users, 'unread');
array_sum($all_unread);
?>
- 2 回答
- 0 關注
- 150 瀏覽
添加回答
舉報
0/150
提交
取消