【问题标题】:Laravel backpack select_from_arrayLaravel 背包 select_from_array
【发布时间】:2016-10-26 18:44:30
【问题描述】:

我完全对 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()
  {
      ..................
  }

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

【问题讨论】:

    标签: php laravel-5.2 laravel-backpack


    【解决方案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,
    
                                        ],
                                 ],
                            );
        }
    

    【讨论】:

    • 非常感谢,对我帮助很大。
    猜你喜欢
    • 2017-05-10
    • 1970-01-01
    • 2020-12-16
    • 2017-11-07
    • 2017-02-01
    • 2020-09-22
    • 2019-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多