zoukankan      html  css  js  c++  java
  • 【转】Laravel 控制器 Controller 传值到 视图 View 的几种方法总结

    单个值的传递

     

    with

    1.  
      public function index() {
    2.  
      $test = "测试";
    3.  
      return view('test.index')->with('test',$test);
    4.  
      }
     

    view

    1.  
      public function index() {
    2.  
      return view('test.index', ['test' => '测试']);
    3.  
      }
     

    compact

    1.  
      public function index() {
    2.  
      $test = "测试";
    3.  
      return view('test.index',compact('test'));
    4.  
      }

     

    多个值的传递

     

    with

    1.  
      public function index() {
    2.  
      return view('test.index')->with(["test1" => "测试1", "test2" => "测试2", "test3" => "测试3"]);
    3.  
      }
     

    view

    1.  
      public function index() {
    2.  
      return view('test.index', ['test1' => '测试1','test2' => '测试2','test3' => '测试3']);
    3.  
      }
     

    compact

    1.  
      public function index() {
    2.  
      $test_1 = "测试1";
    3.  
      $test_2 = "测试2";
    4.  
      $test_2 = "测试3";
    5.  
      return view('test.index',compact('test_1','test_2' ,'test_3' ));
    6.  
      }

     

    数组的传递

     

    with

    1.  
      public function index() {
    2.  
      $data = array( 'test1' => '测试1', 'test2' => '测试2', 'test3' => '测试3' );
    3.  
      return view('test.index')->with($data);
    4.  
      }
     

    view

    1.  
      public function index() {
    2.  
      $data["test1"] = "测试1";
    3.  
      $data["test2"] = "测试2";
    4.  
      $data["test3"] = "测试3";
    5.  
      return view('test.index',$data);
    6.  
      }
     

    compact

    1.  
      //推荐此种方法
    2.  
      public function index() {
    3.  
      $test_array = ["测试1","测试2", "测试2"];
    4.  
      return view('test.index',compact('test_array'));
    5.  
      }

    from :https://www.cnblogs.com/cici1989/p/10723131.html

  • 相关阅读:
    Go复习--为何不允许重载overload?
    Go疑问-1
    Go复习--for循环陷井
    Go复习--slice协程不安全
    Go复习之久违的goto语句
    Go复习---编译错误:undefined:
    Linux 环境拷贝文件发生的错误
    【转载】java数据库操作
    VBA文件处理
    【转】关于C#使用Excel的数据透视表的例子
  • 原文地址:https://www.cnblogs.com/xuan52rock/p/12442348.html
Copyright © 2011-2022 走看看