How to add select_from_array in Backpack for Laravel page/create

1.7k views Asked by At

I want to add a dropdown field picking up an array from config file. I have tried this but getting an error.

In config/gallery.php:

return [
    'column_num' => [
         ''=>'-- Choose one --', 
         'one' => 'Category: One column',
         'one-second'=> 'Category: Two columns',
         'one-third'=> 'Category: Three columns',
         'one-fourth'=> 'Category: Four columns',
         'one-fifth'=> 'Category: Five columns',
         'one-sixth'=> 'Category: Six columns',
    ],
];

In app/PageTemplates.php

...
    $this->crud->addField([
                    'name' => 'column_num',
                    'label' => 'Number of columns',
                    'type' => 'select_from_array',
                    'options' => config('gallery.column_num'),
                    'fake' => true,
                    'store_in' => 'extras',
                ]);

====== UPDATE =====

Error:

ErrorException in a1e88af9db4bb80b9055323bf64be621df2f9960.php line 19: Undefined index: value (View: /Users/sokada/Code/backpack-ceci/vendor/backpack/crud/src/resources/views/fields/select_from_array.blade.php) (View: /Users/sokada/Code/backpack-ceci/vendor/backpack/crud/src/resources/views/fields/select_from_array.blade.php) (View: /Users/sokada/Code/backpack- ceci/vendor/backpack/crud/src/resources/views/fields/select_from_array.blade.php) in a1e88af9db4bb80b9055323bf64be621df2f9960.php line 19 at CompilerEngine->handleViewException(object(ErrorException), '1') in PhpEngine.php line 44 at

I tried this as in the doc, but it still gives an error.

$this->crud->addField([
    // select_from_array
    'name' => 'template',
    'label' => "Template",
    'type' => 'select_from_array',
    'options' => [‘one’ => ‘One’, ‘two’ => ‘Two’],
    'allows_null' => false,
    // 'allows_multiple' => true, // OPTIONAL; needs you to cast this to array in your model;
]);

Error

ErrorException in PageTemplates.php line 114: Use of undefined constant ‘one’ - assumed '‘one’' in PageTemplates.php line 114 at HandleExceptions->handleError('8', 'Use of undefined constant ‘one’ - assumed '‘one’'', '/Users/sokada/Code/backpack-ceci/app/PageTemplates.php', '114', array()) in PageTemplates.php line 114

It is something to do with this if statement.

 <option value="{{ $key }}"
    @if ((isset($field['value']) && $key==$field['value'])
       || ( ! is_null( old($field['name']) ) && old($field['name']) == $key)
       || (is_array($field['value']) && in_array($key, $field['value'])) )
        selected
    @endif
 >{{ $value }}</option>
2

There are 2 answers

0
shin On

For the moment I needed to modify @foreach statement in vendor/backpack/crud/src/resources/views/fields/select_from_array.blade.php to the following to avoid error.

        @if (count($field['options']))
            @foreach ($field['options'] as $key => $value)
                <option value="{{ $key }}"
                    @if ((isset($field['value']) && $key==$field['value'])
                        || ( ! is_null( old($field['name']) ) && old($field['name']) == $key)
                      )
                         selected
                    @endif
                >{{ $value }}</option>
            @endforeach
        @endif

I hope tabacitu will figure it out a better solution.

0
user386093 On

I know that this is an "old" question but I ran into the same error and solved it with this code:

'options' => [0 => "Blocked", 1 => "OK", 2 => "Proxy", 3 => "DNS"],