使用foreach循环在刀片中进行数据呈现

时间:2019-02-13 06:29:05

标签: laravel-5.7

此代码应列出从数据库到刀片文件的所有缺少的专业。数据库查询成功检索到正确的数据,但是未在刀片中呈现吗?

for($i=0;$i<sizeof($majors_array1);$i++)
{ if (School::where('major', '=', $majors_array1[$i])->exists()) 
{ echo $majors_array1[$i] . '       ' ." found"."<br/>";}
else
{ return view('enter-school-dept')->with('majors_array1', 
$majors_array1[$i]);}
}
<table>
<tr>
<th>Title</th>
<th>School</th></tr>
<tr>
@foreach (majors_array1 as $majors) 
<td>$majors</td>
@endforeach
</tr>
</table>

例如 自学考试 电子电气设备

我遇到了错误:

Use of undefined constant majors_array1 - assumed 'majors_array1' (this will throw an Error in a future version of PHP) (View: C:\xampp\htdocs\test\resources\views\enter-school-dept.blade.php) 

1 个答案:

答案 0 :(得分:0)

替换您的foreach:

@foreach (majors_array1 as $majors) 
<td>$majors</td>
@endforeach

使用:

@foreach ($majors_array1 as $major) 
<td>{{ $major }}</td>
@endforeach