【问题标题】:How to Pass array variable to laravel providers directive如何将数组变量传递给 laravel 提供者指令
【发布时间】:2021-02-08 14:32:55
【问题描述】:

我想将数组变量从刀片模板传递给提供者指令函数。数组变量在控制器中创建。

控制器代码

$fields = array(
    0 => array(
        'row' => '',
        'elementArray' => array(
            'formType' => array('type' => 'hidden', 'value' => '', 'name' => 'formType'),
            'id' => array('type' => 'hidden', 'value' => '', 'name' => 'id')
        )
    )
);

return view('welcome',compact('fields'));

刀片文件代码:

@createNewForm($fields)

指令函数

Blade::directive('createNewForm', function ($fields=array()) {
    return $this->createform($fields);
});

输出显示:仅将变量显示为字符串,不显示变量的完整值 $fields

解决办法是什么?

【问题讨论】:

  • 在 AppServiceProvider.php 中查看::share('welcome', $fields);

标签: php laravel


【解决方案1】:

试试这个
您正在刀片中构建宏。你需要返回一个字符串

Blade::directive('createNewForm', function ($this, $fields = array()) {
    return "<?php $this->createform($fields); ?>";
});

【讨论】:

  • 您好,我只是尝试与您更新的相同...但它得到错误:ErrorException Undefined property: App\Providers\GlobalProvider::$createform
  • 我更新了我的答案并将 $this 作为参数发送到 function() ,检查并测试它
猜你喜欢
  • 1970-01-01
  • 2016-03-17
  • 1970-01-01
  • 2020-10-16
  • 2020-06-03
  • 2022-01-11
  • 2018-02-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多