Pooja Krishna Pooja Krishna - 1 month ago 18
PHP Question

Laravel backpack select_from_array

I am totally confused with select_from_array field in laravel backpack.

in my controller i am using a select_from_array field where in options i call a function,but when i run the code error is displayed. please help me with this.

Error : FatalErrorException in EventController.php line 106: syntax error, unexpected '$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()
{
..................
}


Please help me with this , why this happens? how to solve this issue?
Waiting for a response................

Answer

You can not use the pseudo-variable $this out of class method.

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

So if you want to set the attribute of crud's with $this, you can set it in the __construct function

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

Or initialize it the __construct function

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,

                                    ],
                             ],
                        );
    }
Comments