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

    单个值的传递

     

    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'));
    }
  • 相关阅读:
    Hibernate 中出现 XXXX is not mapped 问题
    记录一下表格用poi的导出word
    [转帖] 悟透JavaScript
    JAVA读取Oracle中的blob图片字段并显示
    vs2010代码段,用得飞起~
    FontFamily获取中文名字
    vs2010快捷键
    WPF Binding基础
    Binding对数据的转换和校验
    WPF个UI元素
  • 原文地址:https://www.cnblogs.com/cici1989/p/10723131.html
Copyright © 2011-2022 走看看