将数组从Blade传递给控制器​​:访问元素

时间:2016-09-18 09:21:41

标签: php arrays laravel-5.2 laravel-blade

我正在尝试将数组(源自自定义配置文件)从刀片模板传递到控制器,以将其处理为表单字段。配置数组应该像一个'模板'这样可以通过更改配置文件轻松创建表单。

虽然传递数组似乎工作,但我无法访问它的元素(未定义索引)。

(部分)配置文件

return [

    'tabs' => [

        'Menu'     => [
            'name'    => 'menu',
            'type'    => 'string',
            'length'  => 100,
            'default' => null
        ],
        ...
    ]

]

在我的Blade模板中,我正在迭代此数组,调用控制器函数:

@foreach($tabs as $key => $fld)
    {!! \App\Classes\Fields::createField($key, $fld, $page) !!}
@endforeach

控制器(简称):

class Fields
{
    public static function createField($key, $fld, $page)
    {
        var_dump($fld);

        // Result-------------------------------

        // array (size=4)
        //   'name' => string 'menu' (length=4)
        //   'type' => string 'string' (length=6)
        //   'length' => int 100
        //   'default' => null        }

        var_dump($fld['name']);

        // Result-------------------------------    

        // ErrorException in Fields.php line 19: 
        // Undefined index: name
        // (The other elements also can't be accessed)

}

我真的不明白。 Var_dump显示有效数组,而无法访问它的元素。我把头发拉了几个小时。这是一个Laravel的bug还是我自己的车? ; - )

1 个答案:

答案 0 :(得分:1)

您是否尝试在$fld方法中循环浏览createField()数组以查看结果?这可以让你对这个bug有所了解......

<强> BLADE

    @foreach($tabs as $key => $fld)
        {!! \App\Classes\Fields::createField($key, $fld, $page) !!}
    @endforeach

SIMPLIFIED CONTROLLER

<?php

    class Fields {


        public static function createField($key, $fld, $page){
            // JUST FOR DEBUGGING PURPOSES: TRY THIS & SEE WHAT YOU GET.
            // BUT CHECK FIRST IF $fld IS AN ARRAY
            if( is_array($fld) ){
                foreach($fld as $key=>$value){    
                    var_dump($key);
                    var_dump($value);
                }
            }else{
                // OTHERWISE, TRY DETERMINING THE TYPE OF $fld
                var_dump("\$fld IS AN OBJECT? ", is_object($fld));
                var_dump("\$fld IS A  STRING? ", is_string($fld));
            }

            var_dump($fld);
            var_dump($fld['name']);
        }
    }
  

<强>更新

    <?php

        class Fields {

            public static function createField($key, $fld, $page){
                // CREATE VARIABLES FOR EACH EXPECTED VALUE(S):
                // INITIALIZE THEM TO NULL...
                $name = $type = $length = $default = null;
                // JUST FOR DEBUGGING PURPOSES: TRY THIS & SEE WHAT YOU GET.
                // BUT CHECK FIRST IF $fld IS AN ARRAY
                if( is_array($fld) ){
                    foreach($fld as $key=>$value){
                        switch($key){
                            case "name":
                                $name       = $value;
                                break;
                            case "type":
                                $type       = $value;
                                break;
                            case "length":
                                $length     = $value;
                                break;
                            case "default":
                                $default    = $value;
                                break;                          
                        }                           
                    }
                }
                echo $name;
                echo $type;
                echo $length;
                echo $default;
            }
        }