我有一张叫做“课程”的桌子。我想显示'第一行','第二行'和'第三行'每个用户谁从我的课程表购买了一个或多个课程,每个课程的总分,个别。
这是我的“课程”表-
我的输出是-
我在控制器中尝试了类似bellow的操作,但是我无法将'$totalPoints'变量传递给blade来显示totalPoints,我认为这是错误的方法。我该怎么做才能得到正确的结果?请有人帮帮我-
public function showCourses(){
if ($user_name = Sentinel::getUser()->user_name) {
$mycourses = DB::table('users')
->join('buy_courses', 'users.user_name', '=', 'buy_courses.user_name')
->join('courses', 'courses.course_name', '=', 'buy_courses.course_name')
->select('buy_courses.course_name','courses.amount','courses.first_line','courses.second_line','courses.third_line','buy_courses.user_name')
->where('users.user_name', $user_name)
->get();
//total points for affiliates
$line_points = DB::table('courses')
->select('first_line','second_line','third_line')
->first();
$totalPoints = $line_points->first_line + $line_points->second_line + $line_points->third_line;
}
//dd($totalPoints);
return view('courses.show',['mycourses' => $mycourses],['totalPoints' => $totalPoints]);
}
最佳答案
这很简单。您可以在控制器中获取所有课程后尝试此操作:-
public function showCourses(){
if ($user_name = Sentinel::getUser()->user_name) {
$mycourses = DB::table('users')
->join('buy_courses', 'users.user_name', '=', 'buy_courses.user_name')
->join('courses', 'courses.course_name', '=', 'buy_courses.course_name')
->select('buy_courses.course_name','courses.amount','courses.first_line','courses.second_line','courses.third_line','buy_courses.user_name')
->where('users.user_name', $user_name)
->get();
foreach($mycourses as $key => $getcourse){
$mycourses[$key]->total_points = $getcourse->first_line + $getcourse->second_line + $getcourse->third_line;
}
return view('courses.show',['mycourses' => $mycourses]);
}
现在你不需要分别通过总分。现在总分添加到mycourses键中。当你像这样用刀片打印时-
@foreach($mycourses as $course)
Course Name : {{$course->course_name}}
First Line : {{$course->first_line}}
Second Line : {{$course->second_line}}
Third Line : {{$course->third_line}}
Course Fee : {{$course->amount}}
All Total Points : {{$course->total_points}}
@endforeach
希望有帮助!
关于php - 在laravel Blade Laravel 5.5中找到总计很多列值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46419504/