【问题标题】:How to pass a variable in the View method in Laravel如何在 Laravel 的 View 方法中传递变量
【发布时间】:2019-01-28 23:42:41
【问题描述】:

如何传递一个可以被 Laravel 中的 view() 方法解析的变量?

  public function index($department_id) { 

     $employees=Employee::all();  
     $department=Department::find($department_id); 

     return view('departments.$department->department_code.index')->with('department',$department)->with('employees', $employees); 
  }

这个想法是每个部门都有一个与部门模型中的部门代码同名的部门文件夹。我希望能够接收 department_id,检索正确的部门,然后返回包含部门所有属性的适当视图。

当我尝试这样做时,我收到此错误InvalidArgumentException View [departments.$department->department_code.index] not found.

【问题讨论】:

    标签: laravel view subdomain


    【解决方案1】:
    return view(sprintf('departments.%s.index', $department->department_code), [
        'departments' => $departments,
        'employees' => $employees,
    ]);
    

    【讨论】:

      【解决方案2】:

      如果要在字符串中使用内联变量,则需要使用双引号(")。

      return view("departments.$department->department_code.index")->with('department',$department)->with('employees', $employees);
      

      【讨论】:

        【解决方案3】:

        你可以这样做:

        return view("departments.$department->department_code.index", compact('employees','department'))
        

        【讨论】:

          猜你喜欢
          • 2020-11-03
          • 1970-01-01
          • 2016-05-01
          • 1970-01-01
          • 2014-06-21
          • 2021-03-03
          • 1970-01-01
          • 1970-01-01
          • 2021-09-26
          相关资源
          最近更新 更多