單個值的傳遞
with
-
public function index() {
-
$test = "測試";
-
return view('test.index')->with('test',$test);
-
}
view
-
public function index() {
-
return view('test.index', ['test' => '測試']);
-
}
compact
-
public function index() {
-
$test = "測試";
-
return view('test.index',compact('test'));
-
}
多個值的傳遞
with
-
public function index() {
-
return view('test.index')->with(["test1" => "測試1", "test2" => "測試2", "test3" => "測試3"]);
-
}
view
-
public function index() {
-
return view('test.index', ['test1' => '測試1','test2' => '測試2','test3' => '測試3']);
-
}
compact
-
public function index() {
-
$test_1 = "測試1";
-
$test_2 = "測試2";
-
$test_2 = "測試3";
-
return view('test.index',compact('test_1','test_2' ,'test_3' ));
-
}
數組的傳遞
with
-
public function index() {
-
$data = array( 'test1' => '測試1', 'test2' => '測試2', 'test3' => '測試3' );
-
return view('test.index')->with($data);
-
}
view
-
public function index() {
-
$data["test1"] = "測試1";
-
$data["test2"] = "測試2";
-
$data["test3"] = "測試3";
-
return view('test.index',$data);
-
}
compact
-
//推薦此種方法
-
public function index() {
-
$test_array = ["測試1","測試2", "測試2"];
-
return view('test.index',compact('test_array'));
-
}
from :https://www.cnblogs.com/cici1989/p/10723131.html