在 Laravel 视图中使用 foreach 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47845032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using a foreach variable in a Laravel view
提问by Michael Naidis
My MySQL table looks like this (the relevant part):
我的 MySQL 表看起来像这样(相关部分):
student_id (int) | engmins(int) | totalmins(int) | created_at (datetime)
1 | 50 | 100 | 2017-12-15 00:00:00
1 | 20 | 45 | 2017-12-15 00:00:00
I have the following code:
我有以下代码:
$students = StudentDetails::with('student','studentschool','classactivity')
->where('class', 1)
->orderBy('studentgrade', 'DESC')
->get();
$calculatePercentage = array();
foreach ($students as $s)
{
$fetchEngClassPercents= DB::table('ClassActivity')
->select(DB::raw('round((sum(engmins) * 60) / (sum(totalmins) * 60) * 100) as percents'))
->where('created_at', '>=', Carbon::now()->subDays(30))
->where('student_id', '=', $s->student->id)->get();
foreach($fetchEngClassPercents as $f)
{
$calculatePercentage = $f->percents;
var_dump($calculatePercentage); //Debug purposes
}
}
var_dump($calculatePercentage); //Debug purposes
$params = [
'engClassPercentage' => $calculatePercentage,
'studentInfo' => $students,
];
return view('user.classes.engclass.index', $params);
- var_dump INSIDE the loop executes string(2) "43" NULL
- var_dump OUTSIDE the loop executes NULL
- var_dump 在循环内部执行string(2) "43" NULL
- var_dump OUTSIDE 循环执行NULL
This is how it looks in the view:
这是它在视图中的样子:
<td>
@foreach($engClassPercentage as $p)
{{ $p->percents }}
@endforeach
</td>
This doesn't work in the view, it simply shows nothing.
这在视图中不起作用,它只是什么都不显示。
For some reason, $calculatePercentage remains null outside of the loop (upon previous attempt of dumping the var). When it's in the foreach loop, it executes the query.
出于某种原因, $calculatePercentage 在循环外保持为空(在之前尝试转储 var 时)。当它在 foreach 循环中时,它执行查询。
The strange thing is that I declared the array ($calculatePercentage) outside of the loop are assigned it with the variable in the foreach loop.
奇怪的是,我在循环外声明的数组 ($calculatePercentage) 在 foreach 循环中用变量分配给它。
I am quite lost at this point to be honest, and I'd be glad if I can get assistance.
老实说,我在这一点上很失落,如果我能得到帮助,我会很高兴。
回答by HiKangg
change $calculatePercentage = $f-percents; to like below. Then in foreach part change $p->percents to $p.
改变 $calculatePercentage = $f-percents; 喜欢下面。然后在 foreach 部分将 $p->percents 更改为 $p。
$calculatePercentage[] = $f->percents;
@foreach($engClassPercentage as $p)
{{$p}}
@endforeach
回答by Dave
$calculatePercentage is getting overwritten on each loop change it to an array key
$calculatePercentage 在每个循环中都被覆盖,将其更改为数组键
$calculatePercentage[] = $f->percents;
Then in your foreach call the $params array
然后在你的 foreach 中调用 $params 数组
@foreach($params['engClassPercentage'] as $p)
回答by dexter
You aren't storing values that is getting calculated in foreach loop:
您没有存储在 foreach 循环中计算的值:
Hence the values doesn't persist as per your requirements.
因此,这些值不会按照您的要求持续存在。
Updated Code:
更新代码:
foreach ($students as $s)
{
$fetchEngClassPercents= DB::table('ClassActivity')
->select(DB::raw('round((sum(engmins) * 60) / (sum(totalmins) * 60) * 100) as percents'))
->where('created_at', '>=', Carbon::now()->subDays(30))
->where('student_id', '=', $s->student->id)->get();
foreach($fetchEngClassPercents as $f)
{
$calculatePercentage[] = $f->percents; // <-- This line is edited.
}
}