Laravel背包select_from_array

Laravel backpack select_from_array

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

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

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

controller.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()
  {
      ..................
  }

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

你不能在class方法中使用伪变量$this。

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

The pseudo-variable $this is available inside any class method when that method is called from within an object context. $this is a reference to the calling object

所以如果要用$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,

                                    ],
                             ],
                        );
    }