1. 传递变量
控制器 testController.php
<pre>
class testController extends Controller{
public function index(){
$first = ‘karl’;
$last = ‘song’;
return view(‘test’,compact(‘first’,’last));
}
}
</pre>
模板 test.blade.php
<pre>
{{ $first }} {{ $last }} or {{$first . $last}}
</pre>
2. 传递数组
2.1 索引数组
控制器 testController.php:
<pre>
class testController extends Controller{
public function index(){
$arrayvar = [‘karl’,’badge’,’mettery’ ];
return view(‘test.test’,compact(‘arrayvar’));
}}
</pre>
模板 test.blade.php
例遍输出:
<pre>
@foreach($arrayvar as $vararray)
{{ $vararray }}
@endforeach
</pre>
单独输出:
<pre>
{{ $vararray[0] }}
</pre>
2.2 键值对数组
控制器 testController.php:
<pre>
class testController extends Controller{
public function index(){
$arrayvar = [
‘number1’=>’karl’,
‘number2’=>’badge’,
‘number3’=>’meettry’
];
return view(‘test.test’,compact(‘arrayvar’));
}}
</pre>
模板 test.blade.php
例遍输出:
<pre>
@foreach($arrayvar as $vararray)
{{ $vararray }}
@endforeach
</pre>
单独输出:
<pre>
{{ $vararray[‘number1’] }}
</pre>
注释
键值对数组,亲测没办法用 $vararray[0] ,索引方式输出。