时间:2019-07-04 来源:风信官网 点击: 1325次
从接触编程,到 PHP,总共学习了三个月,刚接触 laravel 框架没多久
对于新人来说,感觉传值有的时候看起来太迷惑。所以在这里整理一下,水平有限,也是第一次用 markdown 写文档有任何错误或者需要改进的地方请诸位悉心赐教。
传值类型:一个值,多个值,数组。
方法: with,view,compact
默认视图 test 文件下 index.blade.php
public function index() {
$test = "测试";
return view('test.index')->with('test',$test);
}
public function index() {
return view('test.index', ['test' => '测试']);
}
public function index() {
$test = "测试";
return view('test.index',compact('test'));
}
public function index() {
return view('test.index')->with(["test1" => "测试1", "test2" => "测试2", "test3" => "测试3"]);
}
public function index() {
return view('test.index', ['test1' => '测试1','test2' => '测试2','test3' => '测试3']);
}
public function index() {
$test_1 = "测试1";
$test_2 = "测试2";
$test_2 = "测试3";
return view('test.index',compact('test_1','test_2' ,'test_3' ));
}
public function index() {
$data = array( 'test1' => '测试1', 'test2' => '测试2', 'test3' => '测试3' );
return view('test.index')->with($data);
}
public function index() {
$data["test1"] = "测试1";
$data["test2"] = "测试2";
$data["test3"] = "测试3";
return view('test.index',$data);
}
//推荐此种方法
public function index() {
$test_array = ["测试1","测试2", "测试2"];
return view('test.index',compact('test_array'));
}
以上就是最近整理出来的一些传值方法,不知道写法上还有什么更聪明的写法。