如何获取刀片内特定id(平均值)的值的数量和总和?



模板:

<div class="item-wrapper" style="text-align:justify; top: 100px;">
<legend>Reviews</legend>
@if (count($reviews)>0)
@forelse($reviews as $review)
@if( $review->counselor_id == $cprofile->id)
{{$review->review}}
{{$review->rating}}
<star-rating :rating="{{$review->rating}}">
</star-rating>
@endif @empty
@endforelse
@endif
</div>

控制器:

$relations = [
'viewCprofile' => counselor::all(),
'reviews' => DB::table('counselor_reviews')
->select('*')
->get()
];
return view('viewCprofile', $relations); 

路线:

Route::get('/viewCprofile', 'ProfilesController@getCprofiles');

我应该将 id 传递给控制器以获取数据吗?

您可以使用以下内容并完成代码

@if (count($reviews)>0) 
@php $total_ratings = 0 @endphp
@forelse($reviews as $review) 
@if( $review->counselor_id == $cprofile->id) 
@php $total_ratings += $review->rating @endphp
{{$review->review}} {{$review->rating}} 
<star-rating :rating="{{$review->rating}}"> </star-rating>
{{$total_ratings}}
@endif

最新更新