1

我完全对 laravel 背包中的 select_from_array 字段感到困惑。

在我的控制器中,我使用了一个 select_from_array 字段,在选项中我调用了一个函数,但是当我运行时,会显示代码错误。请帮我解决一下这个。

错误:EventController.php 第 106 行中的 FatalErrorException:语法错误,意外的 '$this' (T_VARIABLE)

控制器.php

public $crud = array(
    "model" => "App\Larapen\Models\Event",
    "entity_name" => "event",
    "entity_name_plural" => "events",
    "route" => "admin/event",
    "reorder" => true,
    "reorder_label" => "name",
    "reorder_max_level" => 2,
    "details_row" => true,

    // *****
    // COLUMNS
    // *****
    "columns" => [
        [
            'name' => "id",
            'label' => "ID"
        ],
  ],
"fields" => [

            [
                'name' => "event_name",
                'label' => "Event name",
                'type' => "text",
                'placeholder' => "Event Name",
            ],
            [
                'name' => "event_topic",
                'label' => "Event Topic",
                'type' => "text",
                'placeholder' => "Event Topic",
            ],
            [
                'name' => "event_type_id",
                'label' => "Event Type",
                'model' => "App\Larapen\Models\EventType",
                'entity' => "eventType",
                'attribute' => "name",
                'type' => "select",
            ],

            [
                'name' => "about_event",
                'label' => "About event",
                'type' => "ckeditor",
                'placeholder' => "About the Event",
            ],
            [
                'name' => "country_code",
                'label' => "Country",
                'type' => 'select_from_array',
                'options' => $this->countries(),
                'allows_null' => false,

            ],
     ],
);


  public function countries()
  {
      ..................
  }

请帮我解决这个问题,为什么会这样?如何解决这个问题?等待回复......

4

1 回答 1

1

您不能使用伪变量 $this 类外方法。

http://php.net/manual/en/language.oop5.properties.php

当从对象上下文中调用该方法时,伪变量 $this 在任何类方法中都可用。$this 是对调用对象的引用

所以如果想用$this设置crud的属性,可以在__construct函数中设置

public function __construct()
{
    $this->crud['fields'][4] = $this->countries();
}

或者用 __construct 函数初始化它

public $crud;

public function __construct()
{
    $this->crud = array(
                        'model' => 'App\Larapen\Models\Event',
                        'entity_name' => 'event',
                        'entity_name_plural' => 'events',
                        'route' => 'admin/event',
                        'reorder' => true,
                        'reorder_label' => 'name',
                        'reorder_max_level' => 2,
                        'details_row' => true,

                        // *****
                        // COLUMNS
                        // *****
                        'columns' => [
                            [
                                'name' => 'id',
                                'label' => 'ID'
                            ],
                        ],
                        'fields' => [

                                    [
                                        'name' => 'event_name',
                                        'label' => 'Event name',
                                        'type' => 'text',
                                        'placeholder' => 'Event Name',
                                    ],
                                    [
                                        'name' => 'event_topic',
                                        'label' => 'Event Topic',
                                        'type' => 'text',
                                        'placeholder' => 'Event Topic',
                                    ],
                                    [
                                        'name' => 'event_type_id',
                                        'label' => 'Event Type',
                                        'model' => 'App\Larapen\Models\EventType',
                                        'entity' => 'eventType',
                                        'attribute' => 'name',
                                        'type' => 'select',
                                    ],

                                    [
                                        'name' => 'about_event',
                                        'label' => 'About event',
                                        'type' => 'ckeditor',
                                        'placeholder' => 'About the Event',
                                    ],
                                    [
                                        'name' => 'country_code',
                                        'label' => 'Country',
                                        'type' => 'select_from_array',
                                        'options' => $this->countries(),
                                        'allows_null' => false,

                                    ],
                             ],
                        );
    }
于 2016-10-26T10:44:30.600 回答